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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:14:07+00:00 2026-06-18T02:14:07+00:00

I have a C++ console application that takes input from user by displaying a

  • 0

I have a C++ console application that takes input from user by displaying a menu and then acting on it. Everything works fine when the user inputs integer values but when the user inputs a ‘?‘ (which I wanted to handle as a ‘display help’ option) the code starts looping infinitely and does not block on the std::cin>> call. Is this expected? What am I missing here?

int queryAction()
{
  while(1) {
    char selection;
    std::cout << "Action: ";
    std::cin >> selection;

    switch(selection)
    {
      //Some code that acts on the selection
      ...
    }
  }
}

Follow Up:
If I add std::cin.clear() before the call to cin>> all is good. I guess my follow up question is why is an error state set on input that should be a valid ascii character?

  • 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-18T02:14:08+00:00Added an answer on June 18, 2026 at 2:14 am

    You never check whether reading was successful. A fundamental part of any programming is proper error handling. std::cin >> selection; will fail if a char cannot be read from the stream or set the eofbit if the end of the source is reached.

    A simple way to check this is to use something like the following:

    if (!(std::cin >> selection)) // Check if operation failed
    {
        std::cout << "Failed to read!\n"; // Notify user
        std::cin.clear(); // Reset stream
        // Ignore rest of line
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a console application, that when I run (from within vs.net) it takes
I am trying to build a console application that takes user input. I was
I have a console application that is trying to load a CustomConfigurationSection from a
I have a console application with a few methods that: insert data1 (customers) from
I need to process the input from the user in a console application, and
I have a console application that uses a number of command line switches to
I have a console application that should do best effort logging to a database
I have a console application that communicates with a web service. Both of them
I have a console application that sends a XML to a MVC application and
Scenario: I have a console application that needs to access a network share with

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.