0

当我运行它时,它会跳过“输入名字”并直接进入“输入姓氏”。

例子:

输入名字:输入姓氏:Frank

名字是弗兰克

输入电话:

它不允许我输入名字。想法?

if( (fp=fopen("contacts","w")) == NULL )
{
printf( "Failed to open file contacts to write.\n" );
exit( 1 );
}


printf("Enter first name: ");
fgets(first, sizeof(first), stdin);

first[strlen(first) - 1] = '\0';

printf("Enter last name: ");
fgets(last, sizeof(last), stdin);

last[strlen(last) - 1] = '\0';

strcpy(list.name, first);
strcat(list.name, " ");
strcat(list.name, last);

printf("The name is %s\n", list.name);

printf("Enter Phone:");
fgets( line, sizeof( line ), stdin );
sscanf( line, "%s",&list.ph);
printf("You entered: %s", &list.ph);


printf("Enter Address:");
fgets( line, sizeof( line ), stdin );
sscanf( line, "%s", list.add );


printf("Enter Email Address:");
fgets( line, sizeof( line ), stdin );
sscanf( line, "%s", list.email );


printf("\n");


fprintf( fp, "%s\t%s\t%s\t%s", list.name, &list.ph, list.add, list.email );
fclose(fp);
4

2 回答 2

0

我怀疑您将“first”声明为 char 指针,如果您希望 sizeof 有效,则必须将其声明为数组,否则您将获得指针的大小,无论它指向何处

char first[100]; 
printf("Enter first name: ");
fgets(first, sizeof(first), stdin);

免责声明:您没有显示任何变量的声明

于 2012-12-18T22:37:17.920 回答
0

标准输入中可能有一些数据(\n例如 wild )。最好在调试器下运行代码,看看会发生什么。

于 2012-12-19T03:04:53.230 回答