【发布时间】:2015-06-08 10:46:24
【问题描述】:
问题是:
为什么会跳过第一个 fgets 语句? 我在某处读到这可能是因为我以前使用过的 SCANF() 。 我想弄清楚,但我做不到。 谁能给我解决方案(我可能应该重写代码的第一部分以避免scanf,但是如何?)。
这在我正在努力的代码中:
for(;;)
{
//Ask if the user wants to add another CD - Y/N
fputs("\nWould you like to enter new CDs details? y or n\n", stdout);
scanf(" %c" ,&type);
if (toupper(type) != 'Y')
break;
puts("");
//getting in the album information
printf("\tLets enter the details of the CD %d:\n\n", count + 1);
fputs("Title?\n", stdout);
//this fgets statement is being skipped
fgets(title[count], sizeof title[count], stdin);
title[count][strlen(title[count]) - 1] = '\0';
fputs("Atrist? \n", stdout);
fgets(artist[count], sizeof artist[count], stdin);
artist[count][strlen(artist[count]) - 1] = '\0';
}
【问题讨论】:
-
不要混用
scanf()和fgets()。仅使用fgets()进行用户输入。