| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
 本帖最后由 Sa。。 于 2018-9-18 14:40 编辑  
- #include <stdio.h>
 
 - #include <string.h>//为了使用strcmp()
 
 - #include <stdbool.h>
 
 - #include <Windows.h>
 
  
- enum spectrum{red, orange, yellow, green, blue, violet};
 
 - const char * colors[] = { "red", "orange", "yellow", "green", "blue", "violet" };
 
  
- #define LEN 30
 
  
- int main(void)
 
 - {
 
 -         char choice[LEN];
 
 -         enum spectrum color;
 
 -         bool color_is_found = false;
 
  
-         puts("Enter a color (empty line to quit)");
 
 -         while (gets(choice) != 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("Rose 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.");
 
 -                         }
 
 -                 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("Good bye!");
 
  
-         system("pause");
 
 -         return 0;
 
 - }
 
  复制代码 
 
这是C Primer Plus上的一段代码,gets()输入回车以后应该终止读取,但我这个程序没有,不知道怎么回事。如图,要按两次回车才停止读取! 
哈哈哈,我看到你的问题了, while() 后面不可有冒号,你需要多一次回车是因为重新要求输入了 
 
 
 |   
- 
 
 
 
- 
 
 
 
 
 
 
 
 |