当我运行它时,它会跳过“输入名字”并直接进入“输入姓氏”。
例子:
输入名字:输入姓氏: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);