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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:15:19+00:00 2026-06-07T16:15:19+00:00

Possible Duplicate: How to validate numeric input C++ How do you do the following:

  • 0

Possible Duplicate:
How to validate numeric input C++

How do you do the following:

while (iNumberOfPlayers <2 || iNumberOfPlayers >5)
{
    cout << "Enter number of players (1-4): ";
    cin >> iNumberOfPlayers;
    cin.clear();
    std::string s;
    cin >> s;
}

After looking at the loop I’m thrown in, it looks like cin isn’t getting reset (if I put in x) cin reads X again as long as I’m in the while loop. Guessing this is a buffer issue, any way to clear it?

I then tried:

while (iNumberOfPlayers <2 || iNumberOfPlayers >5)
{
    cout << "Enter number of players (1-4): ";
    cin >> iNumberOfPlayers;
    cin.clear();
    cin.ignore();
}

which works except it reads everything 1 at a time. If I put in “xyz” then the loop goes through 3 times before it stops to ask again.

  • 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-07T16:15:21+00:00Added an answer on June 7, 2026 at 4:15 pm

    If the input is not valid, the fail bit is set on the stream. The ! operator used on a stream reads the fail bit (You could also use (cin >> a).fail() or (cin >> a), cin.fail()).

    Then you just have to clear the fail bit before trying again.

    while (!(cin >> a)) {
        // if (cin.eof()) exit(EXIT_FAILURE);
        cin.clear();
        std::string dummy;
        cin >> dummy; // throw away garbage.
        cout << "entered value is not a number";
    }
    

    Please note that if you’re reading from non-interactive input, this would become an infinite loop. So use some variation on the commented error-detection code.

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

Sidebar

Related Questions

Possible Duplicate: php validate integer Currently I'm using this method: Validate the input using
Possible Duplicate: A comprehensive regex for phone number validation How to validate a phone
Possible Duplicate: JQuery validate e-mail address regex hi i have an input filed in
Possible Duplicate: A comprehensive regex for phone number validation Validate phone number with JavaScript
Possible Duplicate: How do I validate that a text input contains only latin letters?
Possible Duplicate: A comprehensive regex for phone number validation i want to validate a
Possible Duplicate: Validate HWND using Win32 API How can I determent if the window
Possible Duplicate: how can i validate a url in javascript using regular expression I
Possible Duplicate: Regular expression to match hostname or IP Address? I need to validate
Possible Duplicate: jquery validate & ajax.beginform I'm trying to use the jQuery validate plugin

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.