当我跑步时,跳过"输入第一个名字"并直接进入"输入姓氏"。
示例:
输入名字:输入姓氏:Frank
名称为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);
答案 0 :(得分:0)
我怀疑你将“first”声明为char指针,如果你想要sizeof工作,你必须将它声明为数组,否则你得到指针的大小,无论它指向何处
char first[100];
printf("Enter first name: ");
fgets(first, sizeof(first), stdin);
免责声明:您没有显示任何变量的decl
答案 1 :(得分:0)
stdin中可能存在一些数据(例如,野生\n
)。最好在调试器下运行代码,看看会发生什么。