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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:47:18+00:00 2026-05-18T02:47:18+00:00

I am currently working on a c++ program and I want to check to

  • 0

I am currently working on a c++ program and I want to check to see if the input the user is making is valid. Currently my code works if the user inputs the proper input or if the user inputs a small incorrect number my pogram will tell the user that the input is invalid. Now my problem is that when the user inputs multiple characters/letters or a large number that has 9 or more digits in it my program goes into an infinate loop giving them the error message. The following is my code:

//for (;;)
    while (flag== false)
    {
        cin >> Input;
        if (Input <= choice.size()-1)
        {
            flag = true;
    //  break;
        }

        else
        {
            cerr << "Input <" << Input << "> is Invalid, Please Choose a Valid Option\n";
            userInput = 0;
        }
    }

As you can see I have also tried doing an infinate for loop but it gives me the same results.
In my code i am printing a vector to the screen. Basicly the user it picking the vectors value to use it.

I am open to any suggestions. Thanks

  • 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-18T02:47:19+00:00Added an answer on May 18, 2026 at 2:47 am

    If the user types in something that can’t be read into Input (it’s not clear from your code what type Input is), that input will get stuck in the input stream and each iteration of the loop will keep failing to read in the input until you clear the stream.

    You need to clear the stream flags and get rid of whatever bad input is waiting in the stream after each failure to read. Try something like this:

    while(!(cin >> Input) || Input <= choice.size()-1)
    {
      cerr << "Input <" << Input << "> is Invalid, Please Choose a Valid Option\n";
      cin.clear(); // Clears the input stream fail flag
      cin.ignore(100, '\n'); // Ignores any characters left in the stream
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a program that has many of those the user SHOULD
i'm working with a multi-threaded program (using pthreads) that currently create a background thread
I'm currently working at a small web development company, we mostly do campaign sites
The company I'm currently working for is using Selenium for Uniting-Testing our User Interface.
I'm currently working on an access control program in C# and I've come across
i am currently working with MEF and facing some problems what i want is
I'm currently working on a program to generate the hashes of files, in Delphi
I am currently working on a program, that can handle Minecraft servers. I am
im currently working in a simple program that implements plugins whith dll libraries (using
I am currently working on getting my program to execute a program (such as

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.