我正在爲學校編寫一個程序,用戶輸入姓名,工作時間和小時工資。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