0

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();
}

I am expecting this output:

Please Enter Your Role 
Quiz Master = 'q' 
Participant = '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.

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)

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.

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

Thanks.

Benoit
  • 76,634
  • 23
  • 210
  • 236
Ishan Sharma
  • 700
  • 2
  • 10
  • 19
  • 2
    `void main()` makes me want to vomit. – Chris Lutz Mar 07 '11 at 05:57
  • Whatever book you're using to learn C is incorrect and apparently incomplete. [Get a new one](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), rather than learning wrong things. The type of `main` is always `int`. – Cody Gray - on strike Mar 07 '11 at 07:10
  • Cody, Well... Tell that to our college professor! Anyway, thanks for pointing out. I will take care. – Ishan Sharma Mar 07 '11 at 10:27

3 Answers3

4

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.

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.

Joshua Olson
  • 3,675
  • 3
  • 27
  • 30
  • Also, please use good formatting for your code in the future. It's hard to read otherwise. I agree with the "makes me want to vomit" comment. – Joshua Olson Mar 07 '11 at 06:04
  • After re-reading the question I realize that your answer is the correct one. – Benoit Mar 07 '11 at 06:09
  • Ah, Sorry for formatting, when I pasted code straight from VC++, editor just took away all the breaks and tabs and displayed code in a big text block. – Ishan Sharma Mar 07 '11 at 06:11
  • Thanks for the reply. So, what should I do in this situation? I am a beginner and I read gidnetwork link you gave but it goes over my head. Any beginner friendly solutions? – Ishan Sharma Mar 07 '11 at 06:14
  • The simplest thing that comes to mind is doing a while(getch()!='\n'); to clear the remaining contents in your buffer before you try and read again. – Joshua Olson Mar 07 '11 at 06:29
  • Please note that getch() is not standard and may not work on a C compiler. – Lundin Mar 07 '11 at 07:39
1

You want

do
{

} while (condition);

As your forgot the semicolon, you get:

do
{
    ....
}

while(condition)
    do something else;

You could have noticed that just by auto-indenting your code in an editor like I did on your question.

Also when you do some scanf you should rather include the \n in the format specification.

Benoit
  • 76,634
  • 23
  • 210
  • 236
0

First of all, # include <stdio.h> and use getc(stdin) to get a character. It'll help you to prevent cursor from moving and putting unnecessary characters to console. Secondly, write the welcome message before the loop.

vissi
  • 2,325
  • 1
  • 19
  • 26