繁体   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