标题:请问这个程序哪里出错了呀,我实在是找不到,当输入的颜色与枚举中的一样时 ...
只看楼主
sunshineli
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-6-21
结帖率:0
已结贴  问题点数:20 回复次数:3 
请问这个程序哪里出错了呀,我实在是找不到,当输入的颜色与枚举中的一样时,程序正常运行,但当输入值与枚举值不一样时,程序停止运行,不返回所规定值
//enum  --  枚举值
# include <stdio.h>
# include <string.h>   // 为了使用strcmp()
# define LEN 30

enum spectrum {red, orange, yellow, green, blue, violet};
const char * colors[] = {"red", "orange", "yellow", "green", "blue", "violet"};

int main(void)
{
    char choice[LEN];
    enum spectrum color;
    int color_is_found = 0;

    puts("Enter a color (empty line to quit): ");

    while(gets(choice) != NULL && choice[0] != '\0')
    {
        printf("choice1 = %s.\n",choice);

        for(color = red; red <= violet; color++)
        {
            printf("choice2 = %s.\n",choice);
            if(strcmp(choice,colors[color]) == 0)
            {
                color_is_found = 1;
                break;
            }
            else
                color_is_found = 0;
        }
        printf("choice3 = %s.\n",choice);
        printf("color_is_found = %d.\n",color_is_found);

        if(color_is_found == 1)
        {
            switch(color)
            {
            case red    : puts("Roses are red.");
                break;
            case orange : puts("Poppies are orange.");
                break;
            case yellow : puts("Sunflowers are yellow.");
                break;
            case green  : puts("Grass are green.");
                break;
            case blue   : puts("Bluebells are bllue.");
                break;
            case violet : puts("Violets are violet.");
                break;
            }
        }
        else
            printf("I don't know about the color %s.\n",choice);

        color_is_found = 0;
        puts("Next color,please (empty line to quit): ");
    }

    puts("Goodbye!");

    return 0;
}
搜索更多相关主题的帖子: 枚举 color choice puts break 
2017-06-21 15:05
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:10 
请问这个程序哪里出错了呀,我实在是找不到
------ 这句话没必要说

当输入的颜色与枚举中的一样时,程序正常运行,但当输入值与枚举值不一样时,程序停止运行,不返回所规定值
------ 与其让别人猜什么叫“输入的颜色与枚举中的一样”,什么叫“正常运行”,……
不如直接说“输入fuck,期待输出I don't know about the color fuck.,实际运行出错。”

red <= violet 改为 color <= violet
2017-06-21 16:39
GBH1
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:5
帖 子:112
专家分:510
注 册:2017-6-13
得分:10 
while中的for循环变量?
for(color = red; red <= violet; color++)
        {
            printf("choice2 = %s.\n",choice);
            if(strcmp(choice,colors[color]) == 0)
            {
                color_is_found = 1;
                break;
            }
            else
                color_is_found = 0;
        }


另外for循环color可以定义为int型即 for(color=0;color<=5;color++)....
不然在VC6.0中编译不过
2017-06-21 17:12
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
以下是引用GBH1在2017-6-21 17:12:54的发言:
另外for循环color可以定义为int型即 for(color=0;color<=5;color++)....
不然在VC6.0中编译不过

估计你用的是C++编译器
2017-06-21 22:04



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




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

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