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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:58:58+00:00 2026-06-12T06:58:58+00:00

Possible Duplicate: how do I validate user input as a double in C++? I

  • 0

Possible Duplicate:
how do I validate user input as a double in C++?

I am new to C++, and I have a function in which I am wanting the user to input a double value. How would I go about insuring that the value input was of the correct datatype? Also, how would an error be handled? At the moment this is all I have:

if(cin >> radius){}else{}

I using `try{}catch(){}, but I don’t think that would the right solution for this issue. Any help would be appreciated.

  • 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-12T06:58:59+00:00Added an answer on June 12, 2026 at 6:58 am

    If ostream& operator>>(ostream& , T&) fails the extraction of formatted data (such as integer, double, float, …), stream.fail() will be true and thus !stream will evaluate to true too.

    So you can use

    cin >> radius;
    if(!cin){
        cout << "Bad value!";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cin >> radius;
    }
    

    or simply

    while(!(cin >> radius)){
        cout << "Bad value!";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    

    It is important to ignore the rest of the line, since operator>> won’t extract any data from the stream anymore as it is in a wrong format. So if you remove

    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    

    your loop will never end, as the input isn’t cleared from the standard input.

    See also:

    • std::basic_istream::ignore (cin.ignore)
    • std::basic_istream::fail (cin.fail())
    • std::numeric_limits (used for the maximum number of ignored characters, defined in <limits>).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: JQuery validate e-mail address regex hi i have an input filed in
Possible Duplicate: php validate integer Currently I'm using this method: Validate the input using
Possible Duplicate: PHP method chaining? I had a function for create user, like cruser
Possible Duplicate: Validate a field depending on another field value in Symfony How can
Possible Duplicate: Ensure User has entered email address string in correct format? I have
Possible Duplicate: Check whether $_POST-value is empty I am using this code to validate
Possible Duplicate: Detect all changes to a <input type=text> (immediately) using JQuery I have
Related/Possible duplicate: How can I validate dates in Perl? I have created a script
Possible Duplicate: Validate email address in Javascript? I have javascript for email validation :
Possible Duplicate: How to validate numeric input C++ How do you do the following:

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.