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 6319721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:51:15+00:00 2026-05-24T15:51:15+00:00

I make a lot of simple console c++ applications and one problem I am

  • 0

I make a lot of simple console c++ applications and one problem I am facing is the input buffer. I have tried cin.ignore and flush(), but they don’t seem to be working for me.

If I have code such as:

cin >> char1;
cin >> char2; 

and I press: 1 (space) 2 (Enter), with just one enter, the 1 is stored to char1 and the 2 is stored to char2.

Sorry if I am a little fuzzy on what I am asking. I will try to edit this question if people don’t understand.

  • 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-24T15:51:16+00:00Added an answer on May 24, 2026 at 3:51 pm

    You could use getline to read the whole line at once, then std::string at if you need the first char, or use an isstringstream if you need the first number.

    char char1;
    std::string input;
    
    getline(std::cin, input);
    if (!std::cin.good()) {
      // could not read a line from stdin, handle this condition
    }
    
    std::istringstream is(input);
    is >> char1;
    if (!is.good()) {
      // input was empty or started with whitespace, handle that
    }
    

    Wrap that in a function if you do it often. With the above, if you hit enter directly (no characters entered), or if you enter data starting with whitespace, is will be !good() so char1 will not have been set.

    Alternatively, after you’ve checked that cin is still good, you could simply:

    if (input.empty()) {
      // empty line entered, deal with this
    }
    char1 = input.at(0);
    

    With that, if the string is non-empty, char1 will be set to the first char if input, whatever that is (including whitespace).

    Note that:

    is >> char1;
    

    will only read the first char, not the first number (same with the input.at() version). So if the input is 123 qze, char1 will receive '1' (0x31 if ASCII), not the value 123. Not sure if that is what you want or not. If it’s not what you want, read to an int variable, and cast properly afterwards.

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

Sidebar

Related Questions

I make a lot of simple single executable applications for various things at home
I make a lot of web applications and from time to time I need
I'm trying to make something simple with Stripes. I've read and heard a lot
Its probarbly a simple 3-tier problem. I just want to make sure we use
I'm following a lot of instructions to make a simple tweet from my app.
I make a lot of rails applications, and there seems to be lots of
I make lot of dealing with RFID cards. As much as there are different
I make a lot of use of boost::scoped_ptr in my code and it is
I make a lot of bugfix and feature branches and in the end merge
I make a lot of use of 'do.call' to generate function calls. E.g: myfun

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.