标题:请问这个程序哪里出错了呀,我实在是找不到,当输入的颜色与枚举中的一样时 ...
取消只看楼主
sunshineli
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-6-21
结帖率:0
已结贴  问题点数:20 回复次数:0 
请问这个程序哪里出错了呀,我实在是找不到,当输入的颜色与枚举中的一样时,程序正常运行,但当输入值与枚举值不一样时,程序停止运行,不返回所规定值
//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



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




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

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