これを実行すると、「名前を入力してください」を飛び越えて、「姓を入力してください」に直行します。
例:
名を入力してください: 姓を入力してください: フランク
名前はフランク
電話番号を入力してください:
名前を入れることはできません。アイデアはありますか?
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);