与fgets()跳过 [英] fgets() skipped

查看:241
本文介绍了与fgets()跳过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写学校的一个程序,用户输入姓名,工作小时数和小时工资。

I am writing a program for school where the user inputs name, hours worked, and hourly wage.

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()输入,直接转到输入每小时收费: scanf()的。我试图改变的顺序把 scanf()的■在与fgets前(),都无济于事。

SIZE is 50, stdio.h & stdlib.h are included. When I compile and move to the A switch it skips the fgets() input and goes straight to the "Enter Hourly Rate: " scanf(). I have tried changing the order putting the scanf()s in front of the fgets(), to no avail.

推荐答案

scanf函数电话留在输入缓冲区的换行符。因此,与fgets 调用时,它读取换行符和回报。

Your scanf call left a newline character in the input buffer. Consequently, when the fgets call is made, it reads that newline and returns.

按PMG的评论上方,检查出的答案问题12.18a 在ç常见问题解答。

As per pmg's comment above, check out the answer to Question 12.18a in the C FAQ.

这篇关于与fgets()跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆