请问这个程序哪里出错了呀,我实在是找不到,当输入的颜色与枚举中的一样时,程序正常运行,但当输入值与枚举值不一样时,程序停止运行,不返回所规定值
//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;
}