标题:C语言初学求助,下面的问题咋处理?
只看楼主
乐天lt
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2017-9-4
结帖率:100%
已结贴  问题点数:20 回复次数:2 
C语言初学求助,下面的问题咋处理?
#include <stdio.h>

char get_first();
float get_num();
char get_choice();

void main()
{
    char cho;
    float a, b;
    while((cho = get_choice()) != 'q')
    {
        printf("Enter first number: ");
        a = get_num();
        printf("Enter second number: ");
        b = get_num();
        switch (cho)
        {
            case 'a': printf("%g + %g = %g\n", a, b, a + b);
                break;
            case 's': printf("%g - %g = %g\n", a, b, a - b);
                break;
            case 'm': printf("%g * %g = %g\n", a, b, a * b);
                break;
            case 'd':
                while (b == 0)
                {
                    printf("Enter a number other than 0: ");
                    b = get_num();
                }
                printf("%g / %g = %g\n", a, b, a / b);
                break;
            default :printf("Program error !\n");
                break;
        }
    }
    printf("Bye!\n");
}

float get_num()
{
    float num;
    char cht;
    while ((scanf("%f", &num)) != 1)
    {
        while ((cht = getchar()) != '\n')
            putchar(cht);
        printf(" is not a num.\n");
        printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
    }
    return num;
}

char get_first()
{
    char ch;
    ch = getchar();
    while ( getchar() != '\n' )
        continue;
    return ch;
}

char get_choice()
{
    char chi;
    printf("Enter the operation of your choice:\n");
    printf("a. add            s. subtract\n");
    printf("m. multiply    d. divide\n");
    printf("q. quit\n");

    chi = get_first();
    while((chi != 'a') && (chi != 's') && (chi != 'm') && (chi != 'd') && (chi != 'q'))
    {
        printf("Please respond with a, s, m, d or q!\n");
        chi = get_first();
    }
    return chi;
}

这个程序中第一次运行正常,第二次输入字符时不能正常运行,直到再输入一次才正常,求助这是怎么回事呀,运行结果如下:
Enter the operation of your choice:\n");
a. add            s. subtract
m. multiply        d. divide
q. quit
a
Enter first number: 22
Enter second number: 1
22 + 1 = 23
d
Please respond with a,s, m, d, q!
d
Enter first number:................(剩下的略过,每次都需要输入两边才能正常执行) 求教这是怎么回事
搜索更多相关主题的帖子: char while printf Enter number 
2017-09-04 19:28
yanzy
Rank: 5Rank: 5
等 级:职业侠客
威 望:2
帖 子:104
专家分:372
注 册:2017-2-7
得分:20 
#include <stdio.h>

char get_first();
float get_num();
char get_choice();

void main()
{
    char cho;
    float a, b;
    while ((cho = get_choice()) != 'q')
    {
        printf("Enter first number: ");
        a = get_num();
        printf("Enter second number: ");
        b = get_num();
        switch (cho)
        {
        case 'a': printf("%g + %g = %g\n", a, b, a + b);
            break;
        case 's': printf("%g - %g = %g\n", a, b, a - b);
            break;
        case 'm': printf("%g * %g = %g\n", a, b, a * b);
            break;
        case 'd':
            while (b == 0)
            {
                printf("Enter a number other than 0: ");
                b = get_num();
            }
            printf("%g / %g = %g\n", a, b, a / b);
            break;
        default:printf("Program error !\n");
            break;
        }
    }
    printf("Bye!\n");
}

float get_num()
{
    float num;
    char cht;
    while ((scanf("%f", &num)) != 1)
    {
        while ((cht = getchar()) != '\n')
            putchar(cht);
        printf(" is not a num.\n");
        printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
    }
    while (getchar() != '\n')//把数值后面的回车消除
        continue;
    return num;
}

char get_first()
{
    char ch;
    ch = getchar();
    while (getchar() != '\n')
        continue;
    return ch;
}

char get_choice()
{
    char chi;
    printf("Enter the operation of your choice:\n");
    printf("a. add            s. subtract\n");
    printf("m. multiply    d. divide\n");
    printf("q. quit\n");

    chi = get_first();
    while ((chi != 'a') && (chi != 's') && (chi != 'm') && (chi != 'd') && (chi != 'q'))
    {
        printf("Please respond with a, s, m, d or q!\n");
        chi = get_first();
    }
    return chi;
}

修改了float get_num()函数,因为你提供数字后遗留了回车
2017-09-05 17:27
乐天lt
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2017-9-4
得分:0 
回复 2楼 yanzy
好的,明白了,谢谢
2017-09-05 20:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-480475-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.067646 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved