Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Validating Input C++

int num = 0;
 
while(true){
  cout << "enter num: ";
  cin >> num;
  if(!(num)){
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout << "num must be an int" << endl;
  }else if(num <= 0){
    cout << "num must be greater than 0" << endl;
  }else if(static_cast<double>(static_cast<int>(num)) != num){
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout << "num must be an int" << endl;
  }else{
    break;
  };
};

I’ve been looking through SO and I found some threads that addressed similar issues, but nothing that is specific to what I’m trying to achieve. I’m only trying to accept integer inputs, no decimals, no strings, no characters. If I enter in a negative number or 0, it’ll throw me an error saying "num must be greater than 0." If I enter in a, it’ll throw me an error saying "num must be an int." If I enter 1.0, it’ll throw me an error saying "num must be an int."

The problems I’m running into with this is when I enter in 0 for example, instead of executing the conditional statement that checks (num <= 0), it runs the conditional statement that says (!(num)). The other problem I’m running into is when I enter in a value that has a decimal, like 2.0, it’ll truncate the numbers after the decimal and send in 2 as the value, completely glossing over the check to see if it’s a decimal value and telling the program that it’s a valid integer when it’s not.

Does anyone have a solution for this, or an article link that solves problem like mines? Thank you!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This:

cin >> num;
if(!(num))

Should be this:

if (!(cin >> num))

You are checking the value of num when you should instead be checking the error state of cin. operator>> returns a reference to the input stream, which is then implicitly convertible to bool, where false means the stream encountered an error.

Also, this is completely useless:

else if(static_cast<double>(static_cast<int>(num)) != num)

Casting an int value to an int is a no-op, and casting an int value to a double back to an int will get you the original int value.

num is an int, it can’t read in anything else. So, by the time your code reaches this point, you know num must be holding a valid int value. However, if the user had actually entered a floating-point number instead, operator>> would have stopped reading at the decimal point, leaving it and the remaining fractional value in cin‘s input buffer for later reading.

If you really need to differentiate between integer and floating-point input, you will have to read in the input as a string first, and then parse it to see what it actually holds, eg:

int num = 0;
string s;
size_t pos;
 
while (true){
  cout << "enter num: ";
  if (!(cin >> s)){
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout << "input error, try again" << endl;
  }
  else{
    try{
      num = stoi(s, &pos);
      if (pos != s.size()) throw invalid_argument("");
      if (num > 0){
        break;
      }
      cout << "num must be greater than 0" << endl;
    }
    catch (const exception &){
      cout << "num must be an int" << endl;
    }
  }
}

Online Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading