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

  • SEARCH
  • Home
  • 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 8939295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:47:39+00:00 2026-06-15T10:47:39+00:00

I’m new to c programming and I’m facing this problem with my program I

  • 0

I’m new to c programming and I’m facing this problem with my program
I have a loop that gets a char form the input buffer

while(c = getchar()){
    if(c == '\n') break;
    if(c == '1') Add();
    if(c == '2') getInput(); // this is where the headache starts
    ....
}

here is the getInput() function

void getInput()
{ 
    char ch = getchar();
    if(ch == '1') doSomething();
    ....
}

but when calling getchar() from the getInput() function it only gets characters that were left in the input buffer from the last call of getchar(). and what i want it to do is to get newly typed characters.

I’ve been googling for two hours for a decent way to clear the input buffer but nothing helped. So a link to a tutorial or an article or something is very appreciated and if there’s another way to implement this then please tell me.

  • 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-06-15T10:47:40+00:00Added an answer on June 15, 2026 at 10:47 am

    First of all there will be == comparison operator rather than = assignment operator in the if condition in this code.

    while(c = getchar()){
        if(c = '\n') break;
        if(c = '1') Add();
        if(c = '2') getInput(); // this is where the headache starts
        ....
    }
    

    And for stop taking input try EOF which from keyboard can be given by prssing CTRL+D.

    EDIT : The problem is with the \n which is actually taken as input when you press ENTER key on the key board. So change just one line of code.

    if (c ==\n) break; to if (c == EOF ) break; and as I said EOF is the end of input.

    Then your code will work fine.

    Flow of code :

    step 1: suppose `2` is input 
    step 2: getInput() is called
    step 3: suppose `1` as input  // in getInput
    step 4: doSomething() is called  // from getInput
    step 5: After completion of doSomething again come back to while loop , 
    
    but in your case you have already given `\n` character as an input 
    
    when you pressed `1` and `ENTER`.And thus loop terminates.
    

    but after changing the code as I said , this should work.

    NOTE: To understand code flow and for debugging purposes it’s best practice to put printf() in various places in functions and see the output as which lines are executing and which are not.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this

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.