poj2328---"right on"进入下一个case的模板(while)

#include <stdio.h>

#include <stdlib.h>

#include<string.h>



int main()

{

    char str[10];

    int n,begin=0,end=11;

    while(scanf("%d",&n) && n)//以0结束的可以由这种格式写

    {

        getchar();//读取缓冲区的东西

        gets(str);

        if(!strcmp(str,"too low") && n>begin)//猜数的人傻,加后面那个条件说明他每一次都像第一次猜

        {

            begin=n;

        }

        if(!strcmp(str,"too high") && n<end)

        {

            end=n;

        }

        if(!strcmp(str,"right on"))

        {

            if(n<end && n>begin)

            {

                printf("Stan may be honest");

            }

            else

            {

                printf("Stan is dishonest");

            }

            begin=0;end=11;//初始化条件

            continue;

        }

    }

    return 0;

}

总结:gets之前如果用了scanf,中间要用getchar

scanf不能用来读并且存储包含空格的串,用gets,但之前一定用getchar清除缓冲区的杂碎

10

too high

3

too low

4

too high

2

right on

5

too low

7

too high

6

right on

0
这种输入的模板代码,read above

 

你可能感兴趣的:(while)