[英]Not displaying all data in C using struct
So I have this simple program in C that I am working on to help me understand structures.所以我在 C 中有这个简单的程序,我正在努力帮助我理解结构。 There is nothing too complicated about it.
没有什么太复杂的。 However when it gets to the point where it should display the data, it only displays part of the data.
但是,当它到达应该显示数据的地步时,它只显示部分数据。 The program asks a user for a first and last name, then an amount.
该程序要求用户输入名字和姓氏,然后是金额。 Then it should display the first and last name, as well as the amount.
然后它应该显示名字和姓氏,以及金额。 It does not display the last name.
它不显示姓氏。 I am sure this is probably something simple, but I am not sure what I am missing here.
我确信这可能很简单,但我不确定我在这里缺少什么。
Here is the code:这是代码:
#include <stdio.h>
#include <stdlib.h>
#define NAMESIZE 30
struct data{
float amount;
char firstName[NAMESIZE];
char lastName [NAMESIZE];
}record;
int main()
{
printf("\nEnter the donor's first and last names \n");
printf("Separate names by a space: ");
scanf("%s, %s", record.firstName, record.lastName);
char c;
while ( (c = getchar()) != '\n' && c != EOF )
{
}
// At this point the program does not work correctly
// It will just print the first name not the last name
printf("\nEnter the donation amount: ");
scanf("%f", &record.amount);
// Display the information
printf("\nDonor %s %s gave $%.2f \n", record.firstName, record.lastName, record.amount);
return 0;
}
Any suggestions on this would be greatly appreciated.对此的任何建议将不胜感激。 Thank you
谢谢
Once I got rid of the extra comma in the first scanf call, it worked.一旦我在第一次 scanf 调用中去掉了多余的逗号,它就起作用了。 Here is the line that got corrected:
这是已更正的行:
scanf("%s %s", record.firstName, record.lastName);
I had a comma between the two %s, and that was incorrect.我在两个 %s 之间有一个逗号,这是不正确的。
Or maybe can use fgets that have buffer overflow protected and use strtok to split the spacing或者也许可以使用具有缓冲区溢出保护的 fget 并使用 strtok 来分割间距
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAMESIZE 30
struct data{
float amount;
char firstName[NAMESIZE];
char lastName [NAMESIZE];
}record;
int main()
{
char *name = malloc(NAMESIZE);
if (name == NULL) {
printf("No memory\n");
return 1;
}
printf("\nEnter the donor's first and last names \n");
printf("Separate names by a space: ");
//scanf("%s, %s", record.firstName, record.lastName);
fgets(name, NAMESIZE, stdin);
if ((strlen(name) > 0) && (name[strlen (name) - 1] == '\n'))
name[strlen (name) - 1] = '\0';
//split name
int init_size = strlen(name);
char delim[] = " ";
char *ptr = strtok(name, delim);
int idx = 0;
while(ptr != NULL)
{
printf("%d '%s'\n",idx, ptr);
if(idx == 0){
strcpy(record.firstName, ptr);
}
else{
strcpy(record.lastName, ptr);
}
ptr = strtok(NULL, delim);
idx += 1;
}
/*
char c;
while ( (c = getchar()) != '\n' && c != EOF )
{
}
*/
// At this point the program does not work correctly
// It will just print the first name not the last name
printf("\nEnter the donation amount: ");
scanf("%f", &record.amount);
// Display the information
printf("\nDonor %s %s gave $%.2f \n", record.firstName, record.lastName, record.amount);
free(name);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.