intmain(void) { char choice[LEN]; enumspectrumcolor;//定义color变量表示枚举符(常量) bool color_is_found = false; puts("Enter a color (empty line to quit):"); while (s_gets(choice, LEN) != NULL && choice[0] != '\0') { for (color = red; color <= violet; color++)//常量的使用 { if (strcmp(choice, colors[color]) == 0) { color_is_found = true; break; } } if (color_is_found) 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 is green."); break; case blue : puts("Bluebells are blue."); break; case violet : puts("Violets are violet."); break; } else printf("I don't know about the color %s.\n", choice); color_is_found = false; puts("Next color, please (empty line to quit):"); } puts("Goodbye!"); return0; }
char * s_gets(char * st, int n) { char * ret_val; char * find; ret_val = fgets(st, n, stdin); if (ret_val) { find = strchr(st, '\n'); // look for newline if (find) // if the address is not NULL, *find = '\0'; // place a null character there else while (getchar() != '\n') continue; // dispose of rest of line } return ret_val; }