Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4033136
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:48:34+00:00 2026-05-20T11:48:34+00:00

I have a small program that which is confusing me. I am trying using

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:48:35+00:00Added an answer on May 20, 2026 at 11:48 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small server program that accepts connections on a TCP or local
I have a small command line program that uses the Team System API. When
I currently have a small Java program which I would like to run both
I have a small utility that I use to download an MP3 file from
Let say I have written a small program that reads file_A and file_B as
I have made a demo small program that I want to deliver to my
I have written a small program using Borland's C++ builder, and along the way,
I have been working now for few days on a small C program which
I am writing a small program in C++ that receives mic input and does
I have a small program to order and sort email messages, outputting to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.