- 论坛徽章:
- 0
|
本帖最后由 KBTiller 于 2012-04-12 11:50 编辑
另一个可能是个人风格问题,无关对错-
- int score;
- while(1)
- {
- // scanf真难用!
- int ret = scanf("%d", &score);
- if(ret < 0)
- {
- fprintf(stderr, "input failed\n");
- return 1;
- }
- else if(ret == 0)
- {
- scanf("%*s");
- fprintf(stderr, "Try to input again, (score >= 0 and <= 100)\n");
- }
- else if(score < 0 || score > 100)
- {
- printf("score = %d\n", score);
- fprintf(stderr, "Try to input again, (score >= 0 and <= 100)\n");
- }
- else break; // 输入OK
- }
复制代码 我喜欢这样写
- int score;
- while(1)
- {
- // scanf真难用!
- int ret = scanf("%d", &score);
- if(ret == EOF)
- {
- fprintf(stderr, "input failed\n");
- return 1;
- }
- if(ret == 0)
- {
- scanf("%*s");
- fprintf(stderr, "Try to input again, (score >= 0 and <= 100)\n");
- continue ;
- }
- if(score < 0 || score > 100)
- {
- printf("score = %d\n", score);
- fprintf(stderr, "Try to input again, (score >= 0 and <= 100)\n");
- continue ;
- }
- break; // 输入OK
- }
复制代码 |
|