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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:29:55+00:00 2026-06-13T13:29:55+00:00

Update I found the cause of the problem. I have been experimenting with the

  • 0

Update

I found the cause of the problem. I have been experimenting with the fish shell. I saw the comment that said someone had successfully run my code on a Mac, and decided to try it in a standard bash shell. It worked perfectly. So, no more fish shell I guess. 🙂

I would still appreciate knowing how and why cin works the way it does. This is the main part of my question.


I have run into a popular problem: using cin in a loop. The code is straightforward, but I cannot find a solution. The answers Google have provided me are varied, but usually involve some combination of cin.clear(), cin.ignore(), and cin.get(). Unfortunately, I haven’t been able to find a combination or ordering of those that fix my problem. Moreover, I find it frustrating that I don’t have a complete understanding of the function of cin. I’d rather not use trial and error to fix this. I want to understand exactly what’s going on.

What Should Happen

When I run my code, I should see a prompt with a list of options. I should be able to type a character which will run one of the options. Then, it should display the prompt again and repeat the process until I select the Exit option.

What Actually Happens

As soon as I run the code, it prints the prompt to the screen an arbitrary number of times and eventually stops halfway through the prompt. Then I am unable to do anything but kill it with ^C.

$ ./run

Choose an option:
[A]dd a score
[R]emove a player
[E]xit

    : That is not a valid input.

[repeated a bunch of times]

Choose an option:
[A]dd a score
[R]emove a player
[E]xit

    : That is not a valid input.


Choose ^C
$ 

My Question

What causes cin to do that? As an experienced Java developer (but a beginner C++ developer), I’m familiar with the concepts of buffers and streams and such, but I have no idea how cin works. I know that cin.clear() clears an error state, and cin.ignore() ignores a number of characters in the stream. My Google-fu has thus far been unable to find a concise reference.

Why does cin act the way it does? How should one visualize what happens under the hood when using cin in a loop? What is the most elegant way to implement this infinite menu idea in C++?

My Code

Here is a simplified version of my code, which produces the exact same problem as the full version:

#include <iostream>

using namespace std;

int main () {

    //infinite menu 
    char input;
    while(true) {

        //prompt
        cout << "\n\nChoose an option:\n";
        cout << "[A]dd a score\n";
        cout << "[R]emove a player\n";
        cout << "[E]xit\n";
        cout << "\n\t: ";

        //input
        cin >> input;

        //decide what the input means
        switch(input) {
            case 'a':
            case 'A':
                cout << "Add a score.\n";
                break;
            case 'r':
            case 'R':
                cout << "Remove a player.\n";
                break;
            case 'e':
            case 'E':
                cout << "Program Complete.\n";
                return 0;
                break;
            default:
                cout << "That is not a valid input.\n";
        }
    }
    return 0;
}

I compile and run with:

$ g++ Test.cpp -o run
$ ./run

I’m running gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00) on Mac OS X 10.8.2.

  • 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-13T13:29:56+00:00Added an answer on June 13, 2026 at 1:29 pm

    Do yourself a favour and don’t extract tokens directly from std::cin. Instead, read line by line with getline and then interpret each line. Also, you must always evaluate the result of an input operation in a boolean context, or you will not be able to handle arbitrary input correctly.

    For a test, your program must survive if you call echo "abc" | ./run. This should always be one of your first tests.

    Now, on to the code:

    #include <string>
    #include <iostream>
    
    int main()
    {
        for (std::string line; std::getline(std::cin, line); )
        {
            if (line.empty()) { continue; }
    
    
            if (line == "A" || line == "a") { /* ... */ continue; }
    
            if (line == "R" || line == "r") { /* ... */ continue; }
    
            if (line == "E" || line == "e") { break; }
    
            std::cout << "Sorry, I did not understand.\n";
        }
    
        std::cout << "Goodbye!\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found something that works with updating one field at here: http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/ UPDATE person
I found some problem. When i running apc_store and more times update a page
Right guys, all autocomplete plugins and functions that I've found, they only update upon
I have a C# application that uses an unmanaged C++ DLL. I've found a
NOTE: This is the simple version of my previous question that SHOULD have been
I have been working in a project that have been working wothount problems till
UPDATE - Cause found! ... please read below & suggest the solution: While creating
I know that hundreds of variations of these considerations have been posted all over
I have been working on code that parses external XML-files. Some of these files
I have a problem with the 'update' hook. In the case of a new

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.