标题:帮我看下这代码,有一功能不知道该怎实现
只看楼主
DarylL
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2012-12-6
结帖率:87.5%
已结贴  问题点数:10 回复次数:8 
帮我看下这代码,有一功能不知道该怎实现
第一组非数值时全字母,结果显示正确,第二组非数字字母开头,存在数值,显示也正确,但是当数值开头带字符,显示就不正确了,这个该怎么改?求帮忙看下,谢谢了! 运行图和代码附下:
 


#include <stdio.h>

int main (void)
{
    float first;
    float second = 0.0;
    char first_ch;
    char second_ch;
    char choice;

    printf ("Enter  the operation of your choice:\n");
    printf ("a. add           s. subtract\n");
    printf ("m. multiply      d. divide\n");
    printf ("q. quit\n");
    while ((choice = getchar ()) != 'q')
    {
        if (choice == '\n')
            continue;
        else if (choice == 'a' || choice == 's' || choice == 'm' || choice == 'd')
        {
            printf ("Enter first number:");
            while (scanf ("%f", &first) != 1)
            {
                while ((first_ch = getchar ()) != '\n')
                    putchar (first_ch);
                printf (" is not a number.\n");
                printf ("Please enter a number such as 2.5, -1.78E8 or 3:");
            }
            printf ("Enter second number:");
            if (choice = 'd')
            {
                while (second == 0)
                {
                    while (scanf ("%f", &second) != 1)
                    {
                        while ((second_ch = getchar ()) != '\n')
                            putchar (second_ch);
                        printf (" is not a number.\n");
                        printf ("Please enter a number such as 2.5, -1.78E8 or 3:");
                    }
                    if (second == 0)
                        printf ("Enter a number other than 0:");
                }
            }
            else
            {
                while (scanf ("%f", &second) != 1)
                {
                    while ((second_ch = getchar ()) != '\n')
                        putchar (second_ch);
                    printf (" is not a number.\n");
                    printf ("Please enter a number, such as 2.5, -1.78E8 or 3:");
                }
            }
        }
        else
        {
            printf ("Please enter a, s, m, d or q:");
            continue;
        }
        switch (choice)
        {
            case 'a': printf ("%g + %g = %g\n", first, second, first + second);
                break;
            case 's': printf ("%g - %g = %g\n", first, second, first - second);
                break;
            case 'm': printf ("%g * %g = %g\n", first, second, first * second);
                break;
            case 'd': printf ("%g / %g = %g\n", first, second, first / second);
                break;
        }
        printf ("Enter  the operation of your choice:\n");    /*    输入提示    */
        printf ("a. add           s. subtract\n");
        printf ("m. multiply      d. divide\n");
        printf ("q. quit\n");
        second = 0.0;
    }
    
    return 0;
}

[ 本帖最后由 DarylL 于 2013-2-16 10:32 编辑 ]
搜索更多相关主题的帖子: 字母 first include 运行图 choice 
2013-02-16 10:29
心灯甚亮
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:441
专家分:996
注 册:2013-1-29
得分:0 
你先看看if语句里的比较运算服对着不
没测试一下???
2013-02-16 11:10
心灯甚亮
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:441
专家分:996
注 册:2013-1-29
得分:0 
之所以数字加字母会出问题是因为由于先有数字,scanf就把数字读取了,剩下字母留在缓冲区里
2013-02-16 11:15
心灯甚亮
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:441
专家分:996
注 册:2013-1-29
得分:10 
fflush(stdin);清空输入缓冲区,在输入了第一个数后使用即可
2013-02-16 11:21
DarylL
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2012-12-6
得分:0 
回复 4楼 心灯甚亮
我想是输入数字+字母后显示输入错误并重新输入。
2013-02-16 11:25
心灯甚亮
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:441
专家分:996
注 册:2013-1-29
得分:0 
那你只能先把东西存入字符串,然后逐个字符检测是否是数字,一旦发现不是,就提示重新输入。
如果这个字符串合法,就用sscanf()函数从这个字符串里输入这个数

sscanf(const char * src,const char * format,...);
第一个参数是原字符串,后面两个参数和scanf()里边一样的。它从字符串里输入数据,而scanf()从stdin输入数据


[ 本帖最后由 心灯甚亮 于 2013-2-16 11:30 编辑 ]
2013-02-16 11:28
DarylL
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2012-12-6
得分:0 
回复 6楼 心灯甚亮
是不是用这个替换putchar?
2013-02-16 14:11
心灯甚亮
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:441
专家分:996
注 册:2013-1-29
得分:0 
对,但不是简单的直接替换
2013-02-16 15:55
DarylL
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2012-12-6
得分:0 
回复 8楼 心灯甚亮
谢谢~
2013-02-16 16:11



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




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

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