【发布时间】:2011-09-01 21:41:36
【问题描述】:
我正在为学校编写一个程序,用户输入姓名、工作时间和小时工资。
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;
SIZE 为 50,包括 stdio.h 和 stdlib.h。当我编译并移动到 A 开关时,它会跳过 fgets() 输入并直接转到 "Enter Hourly Rate: " scanf()。我尝试更改将scanf()s 放在fgets() 前面的顺序,但无济于事。
【问题讨论】:
-
阅读the c-faq 的第 12 节。而且,既然你已经在那里了,也请阅读其他部分:-)