pepsi02
pepsi02
采纳率77.8%
2021-02-28 18:37

c语言,输入验证方面的问题。

#include<stdio.h>
#define worktime 40
void print(void);
char choice1(void);
int time1(void);
int main(void)
{
    int choice = 0;
    double base = 0;
    int time = 0;
    double rate = 0;
    char ch; double sum = 0, net = 0;
    print();
    printf("enter a letter for your choice:\n");
    while ((choice = choice1()) != 'q')
    {
        switch (choice)
        {
        case 'a':base = 8.75; break;
        case 'b':base = 9.33; break;
        case 'c':base = 10.00; break;
        case 'd':base = 11.20; break;
        default:break;
        }
        printf("enter the work time:\n");
        time = time1();
        if (time < worktime)
            sum = base*time;
        else sum = base*worktime + (time - worktime)*1.5*base;
        if (sum < 300)
            rate = sum*0.15;
        if (sum < 450 && sum>300)
            rate = 300 * 0.15 + (sum - 300)*0.2;
        if (sum > 450)
            rate = 300 * 0.15 + (sum - 300)*0.2 + (sum - 450)*0.25;
        net = sum - rate;
        printf("you have wage:%f,rate:%f,net:%f\n", sum, rate, net);
        printf("\n"); printf("\n"); printf("\n");
        print();
        printf("enter a letter for your choice:\n");
    }
    return 0;
}
void print(void)
{
    printf("enter your choice form a to d and q is quit:\n");
    printf("*******************************************************\n");
    printf("pay rate or action\n");
    printf("a)$8.75/hr   b)$9.33/hr    c)$10.00/hr\n");
    printf("d)$11.20/hr                q)quit\n");
    printf("*******************************************************\n");
}
char choice1(void)
{
    int ch;
    ch = getchar();
    while (getchar() != '\n')
        continue;
    while ((ch<'a' || ch>'d') && ch != 'q')
    {
        printf("please respond with a,b,c,d or q\n");
        ch = getchar();
    }
    return ch;

}
int time1(void)
{
    int time, ch;
    while (scanf("%d", &time) != 1)
    {
        while ((ch = getchar()) != '\n')
            putchar(ch);
        printf(" is not an integer,please enter an integer.\n");
    }
    return time;
}

有没有大佬有空,帮我看看,为什么第二次输入验证会失败。研究了3天,实在头痛啊!!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • pepsi02 pepsi02 2月前

    唉,都没人回答,我自己回答吧,突然灵光一动发现问题所在了.下面是正确代码,虽然写的比较乱。

    #include<stdio.h>
    #define worktime 40
    void print(void);
    char choice1(void);
    int time1(void);
    int getfirst(void);
    int main(void)
    {
        int choice = 0;
        double base = 0;
        int time = 0;
        double rate = 0;
        char ch; double sum = 0, net = 0;
        print();
        printf("enter a letter for your choice:\n");
        while ((choice = choice1()) != 'q')
        {
            switch (choice)
            {
            case 'a':base = 8.75; break;
            case 'b':base = 9.33; break;
            case 'c':base = 10.00; break;
            case 'd':base = 11.20; break;
            default:printf("program error"); break;
            }
            printf("enter the work time:\n");
            time = time1();
            if (time < worktime)
                sum = base*time;
            else sum = base*worktime + (time - worktime)*1.5*base;
            if (sum < 300)
                rate = sum*0.15;
            if (sum < 450 && sum>300)
                rate = 300 * 0.15 + (sum - 300)*0.2;
            if (sum > 450)
                rate = 300 * 0.15 + (sum - 300)*0.2 + (sum - 450)*0.25;
            net = sum - rate;
            printf("you have wage:%f,rate:%f,net:%f\n", sum, rate, net);
            printf("\n"); printf("\n"); printf("\n");
            print();
            printf("enter a letter for your choice:\n");
        }
        return 0;
    }
    void print(void)
    {
        printf("enter your choice form a to d and q is quit:\n");
        printf("*******************************************************\n");
        printf("pay rate or action\n");
        printf("a)$8.75/hr   b)$9.33/hr    c)$10.00/hr\n");
        printf("d)$11.20/hr                q)quit\n");
        printf("*******************************************************\n");
    }
    char choice1(void)
    {
        int ch;
        ch = getfirst();

        while ((ch<'a' || ch>'d') && ch != 'q')
        {
            printf("please respond with a,b,c,d or q\n");
            ch = getfirst();
        }
        return ch;

    }
    int time1(void)
    {
        int time, ch;
        while (scanf("%d", &time) != 1)
        {
            while ((ch = getchar()) != '\n')
                putchar(ch);
            printf(" is not an integer,please enter an integer.\n");
        }
        while (getchar() != '\n')
            continue;
        return time;
    }
    int getfirst(void)
    {
        int ch;
        ch = getchar();
        while (getchar() != '\n')
            continue;
        return ch;

    }

    点赞 评论 复制链接分享