C:do-whil​​e循环重复的太多了! [英] C: Do-While Loop Repeating Too Much!

查看:99
本文介绍了C:do-whil​​e循环重复的太多了!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序,这是混淆了我。我使用的是循环接受来自用户的输入尝试。如果输入错误,则再次重复,但如果它是正确的,它退出。
在code段是:

I have a small program that which is confusing me. I am trying using a loop to take input from user. In case input is wrong, it is repeated again but if it is right, it exits. The code snippet is:

void main()
{
    char user_status; // Checks User Status q = Quiz Master and p = Participant
    int valid_status = '0'; // Checks If User Status Is Valid Or Not. Used In Some Loops. 0 = Invalid, 1 = Invalid.
    printf("Welcome to General Knowledge Quiz Management System.\nThis application has been designed to help you conduct a quiz or test your GK.");
    do
    {
        user_status = '0';
        printf("\n\nPlease enter your role.\nQuiz Master = \'q\'\nParticipant = \'p\'\n");
        scanf("%c", &user_status);
        if (user_status == 'q'|| user_status == 'Q')
        {
            printf("Initializing Quiz Master Segment\n\n________________________________\n");
            initiate_qm();
            valid_status = '1';
        }
        else if (user_status == 'p' || user_status == 'P')
        {
            printf("Initializing Participant Segment");
            initiate_pa();
            valid_status = '1';
        }
    }
    while (valid_status != '1')
        printf("\nProgram Will Exit Now. Press Any Key To Return To Windows.");
    getch();
}

我期待这样的输出:

Please Enter Your Role 
Quiz Master = 'q' 
Participant = 'p'

到现在为止,它的伟大工程。当我输入Q / Q / P / P,它的伟大工程。但是,当我输入一些错误,它不给所需的输出。

Till now, it works great. When I input q/Q/p/P, it works great. But when I input something wrong, it does not give required output.

例如,如果我输入ABC,我应该得到上面的文字再次要求我输入q或页。但是相反,我得到这样的:

For example, if I input "abc", I should get the above text again asking me to input q or p. But instead, I get this:

 Please Enter Your Role 
 Quiz Master = 'q' 
 Participant = 'p' 
 Please Enter Your Role 
 Quiz Master = 'q' 
 Participant = 'p'
 Please Enter Your Role 
 Quiz Master = 'q' 
 Participant = 'p'
 Please Enter Your Role 
 Quiz Master = 'q' 
 Participant = 'p'
 _ (I have to input here)

现在,它为什么重复3倍多。需要注意的一个有趣的事情是,如果我输入的东西是长2个字符,它重复2倍多,如果我留空白(刚打回),不重复多余的时间。

Now, why is it repeating 3 extra times. One interesting thing to note is that if I input something that is 2 characters long, it repeats 2 extra times and if I leave it blank(just hit return), it does not repeat extra times.

我只使用C.我使用Visual C ++ 2010编译。

I have to use only C. I am using Visual C++ 2010 to compile.

感谢。

推荐答案

由于您已scanf的三个字符处理给出。它消除了第一首字符第一次调用scanf函数得到'A',但仍然有在标准输入缓冲区BC离开了。

Because you have given scanf three characters to process. It removes first first character the first time it calls scanf getting 'a', but still has 'bc' left in the stdin buffer.

您需要检查吃剩的东西在你的缓冲区你看看重新输入之前。我会避免刷新标准输入缓冲区,因为它是不确定的行为。 (http://www.gidnetwork.com/b-57.html)

You need to check for leftover stuff in your buffer before you look for input again. And I'd avoid flushing the stdin buffer because it's undefined behavior. (http://www.gidnetwork.com/b-57.html)

您可以阅读余下的字符,并丢弃

You can read the remaining characters and discard them with

do{  
    scanf("%c", &user_status);  
}while(user_status!='\n'); //This discards all characters until you get to a newline

你读你想要的字符之后。

right after you read the character you want.

这篇关于C:do-whil​​e循环重复的太多了!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆