我正在为学校编写一个程序,用户输入姓名,工作时间和小时工资。fgets()跳过
int main()
{
char name[SIZE];
char selection = 'Z';
int hoursWorked = 0,
counter = 0,
flag = 1;
float hourlyRate = 0.0;
const float otRate = 1.5;
const int week = 40;
while (flag == 1)
{
system ("cls");
printf ("\n\tP A Y R O L L P R O G R A M\n");
printf ("---------------------------------------------\n");
printf ("\t(A) - New Payroll Info\n\t(B) - Display Payroll\n\t(C) - Quit\n\t");
scanf (" %c", &selection);
selection = toupper (selection);
switch (selection)
{
case 'A':
system ("cls");
printf ("Enter Employee Name: ");
fgets (name, SIZE, stdin);
strip_newline(name, 50);
printf ("\nEnter Hourly Rate: ");
scanf ("%f", &hourlyRate);
printf ("\nEnter Hours Worked This Week: ");
scanf ("%d", &hoursWorked);
system ("pause");
break;
大小为50,stdio.h中& stdlib.h中都包括在内。当我编译并移动到A开关时,它将跳过fgets()
输入并直接进入"Enter Hourly Rate: "
scanf()
。我试图改变这个命令,把scanf()
放在fgets()
的前面,但是没有用。
阅读[c-faq](http://c-faq.com/)的第12部分。而且,既然你已经在那里了,请阅读其他章节:-) – pmg