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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:02:52+00:00 2026-06-15T03:02:52+00:00

I get the loop, and the initial prompt for input of type int, but…

  • 0

I get the loop, and the initial prompt for input of type int, but… what is the while loop checking with !(cin >> [variable])? I looked at cin on cplusplus.com for an explanation but I don’t see it holding any value … it looks like it’s just checking the numerical value of the number entered, how would that check for a valid integer input?

int number;
.
.
.
    cout<<"Please enter a number: ";
    while (!(cin >> number))
    {
        cin.clear();
        cin >> badinput;
        cout <<"Input " << badinput << " is invalid, please enter a number: ";
    }
  • 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-15T03:02:54+00:00Added an answer on June 15, 2026 at 3:02 am

    operator >> on an input stream returns a reference to the stream, not the thing read from the stream. The value read is placed into what the argument references, which must be an lvalue.

    The ! operator on an iostream tests the failure flag on on the stream — it returns true if there’s been any sort of failure on the stream. For an input stream, if you attempt to read an integer (as this code is doing) and the input doesn’t look like an integer, the fail flag will be set.

    So this code (attempts to) read an integer into number and then tests the fail flag. If it’s set, it clears the fail flag, reads badinput (probably a string), prints the message and loops, attempting to read another integer.

    So as long as the only failure is with the formatting, it will loop until it gets an integer. If there’s some other more persistent failure (eg, if cin gets an EOF or is closed), it will loop forever.

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

Sidebar

Related Questions

hi im doing a loop so i could get dict of data, but since
Just trying to get some rows out of a database and loop through but
Okay When I was making macros I couldn't get the loop to work proper
I am creating a chart control chart1.ChartAreas.Add(area); chart1.series.add(time); //loop //get values of a and
I'm using a foreach loop to get images from the graph API in facebook.
I remove item from ArrayList in foreach loop and get follwing exception. Collection was
In the NHibernate, how to loop and get results ? for example, how to
I am have two xml files.. I first get one and loop through it
There's a loop where we get certain data. Depending on the data, file writer
I have to loop in a JSON array to get some informations in node

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.