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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:37:32+00:00 2026-05-31T17:37:32+00:00

Suppose we have a menu which presents the user with some options: Welcome: 1)

  • 0

Suppose we have a menu which presents the user with some options:

Welcome:
1) Do something
2) Do something else
3) Do something cool
4) Quit

The user can press 1 – 4 and then the enter key. The program performs this operation and then presents the menu back to the user. An invalid option should just display the menu again.

I have the following main() method:

int main()
{
    while (true)
        switch (menu())
        {
            case 1:
                doSomething();
                break;
            case 2:
                doSomethingElse();
                break;
            case 3:
                doSomethingCool();
                break;
            case 4:
                return 0;
            default:
                continue;
        }
}

and the follwing menu():

int menu()
{
  cout << "Welcome:" << endl
       << "1: Do something" << endl
       << "2: Do something else" << endl
       << "3: Do something cool" << endl
       << "4: Quit" << endl;

  int result = 0;
  scanf("%d", &result);
  return result;
}

Entering numerical types works great. Entering 1 – 4 causes the program to perform the desired action, and afterwards the menu is displayed again. Entering a number outside this range such as -1 or 12 will display the menu again as expected.

However, entering something like ‘q’ will simply cause the menu to display over and over again infinitely, without even stopping to get the user input.

I don’t understand how this could possibly be happening. Clearly, menu() is being called as the menu is displayed over and over again, however scanf() is part of menu(), so I don’t understand how the program gets into this error state where the user is not prompted for their input.

I originally had cin >> result which did exactly the same thing.

Edit: There appears to be a related question, however the original source code has disappeared from pastebin and one of the answers links to an article which apparently once explained why this is happening, but is now a dead link. Maybe someone can reply with why this is happening rather than linking? 🙂

Edit: Using this example, here is how I solved the problem:

int getNumericalInput()
{
    string input = "";
    int result;

    while (true)
    {
        getline(cin, input);

        stringstream sStr(input);
        if (sStr >> result)
            return result;

        cout << "Invalid Input. Try again: ";
    }
}

and I simply replaced

int result = 0;
scanf("%d", &result);

with

int result = getNumericalInput();
  • 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-31T17:37:33+00:00Added an answer on May 31, 2026 at 5:37 pm

    When you try to convert the non-numeric input to a number, it fails and (the important part) leaves that data in the input buffer, so the next time you try to read an int, it’s still there waiting, and fails again — and again, and again, forever.

    There are two basic ways to avoid this. The one I prefer is to read a string of data, then convert from that to a number and take the appropriate action. Typically you’ll use std::getline to read all the data up to the new-line, and then attempt to convert it. Since it will read whatever data is waiting, you’ll never get junk “stuck” in the input.

    The alternative is (especially if a conversion fails) to use std::ignore to read data from the input up to (typically) the next new-line.

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

Sidebar

Related Questions

Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
I have a menu which contains a delete button to delete items from it.
I have a page with a header, followed by a (menu/tool) bar underneath, which
I have a side menu in my site, which is currently a load of
I have number of drop down menu's which gets called with a onClick event
I have a menubutton, which when clicked should display a menu containing a specific
I have a menu structure that is supposed to highlight each item as it
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose we have a table A: itemid mark 1 5 2 3 and table

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.