簡體   English   中英

跳過第一項

[英]First entry being skipped

當我運行它時,它會跳過“輸入名字”,然后直接轉到“輸入姓氏”。

例:

輸入名字:輸入姓氏: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);

我懷疑您將“ first”聲明為char指針,如果希望sizeof有效,則必須將其聲明為數組,否則無論指針指向何處,都將獲得指針的大小

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

免責聲明:您未顯示任何變量的decl

stdin中可能有一些數據(例如,通配符\\n )。 最好在調試器下運行代碼,看看會發生什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM