How to check if the input is a valid integer without any other chars?

user2699298
#include <iostream>
#include <limits>

using namespace std;

int main()
{
    int x;
    cout << "5 + 4 = ";
    while(!(cin >> x)){
        cout << "Error, please try again." << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    if (x == (5 + 4)){
        cout << "Correct!" << endl;
    }
    else{
        cout << "Wrong!" << endl;
    }
    return 0;
}

How can I check if the user inputs a valid integer? In this program I wrote above, if the user inputs 9, it should be correct, however, if the user inputs 9a for example, it should return an error, but it doesn't for some reason. How can I correct it?

How I did it using cin.peek()

#include <iostream>
#include <limits>
#include <stdio.h>

using namespace std;

int main()
{
    int x;
    bool ok;
    cout << "5 + 4 = ";

    cin >> x;

    while(!ok){
  cin >> x;

  if(!cin.fail() && (cin.peek() == EOF || cin.peek() == '\n')){
  ok = true;
  }
  else{
  cout << "Error, please try again." << endl;
  cin.clear();
  cin.ignore(numeric_limits<streamsize>::max(), '\n');
  }
    }

    if (x == (5 + 4)){
  cout << "Correct!" << endl;
    }
    else{
  cout << "Wrong!" << endl;
    }

    return 0;
}
Angew is no longer proud of SO

You could read a string, extract an integer from it and then make sure there's nothing left:

std::string line;
std::cin >> line;
std::istringstream s(line);
int x;
if (!(s >> x)) {
  // Error, not a number
}
char c;
if (s >> c) {
  // Error, there was something past the number
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to check if the input is a valid integer without any other chars?

From Dev

How to check if a string is a valid integer?

From Dev

How to check if a string is a valid integer?

From Dev

How to check the input is an integer or not in Java?

From Dev

Check for valid integer

From Dev

Check if char array is not garbage? (valid chars)

From Dev

How do I make this program check if an input is an integer without causing an error in runtime and stop the loop with a sentinel value?

From Dev

How to check if a request is for javascript (or other types) when using Node.js without using any middleware?

From Dev

How to make an integer larger than any other integer?

From Dev

How do I check an input for an integer in Python?

From Dev

How do I check to see if the input is an integer?

From Dev

How to check that the entered input is an integer or not in java?

From Dev

How can I check for invalid input and loop until the input is valid?

From Dev

Is it possible to check if the input is an integer without hasNextLine and exit loop if it's 0?

From Dev

Function to check if an integer is valid in PHP

From Dev

Check 4 chars with regex Ruby console input

From Dev

How to check that none of the chars are in the string?

From Dev

Check if input is integer

From Dev

Check for input being an integer

From Dev

How to check the access token is valid or not without using appId and secret?

From Dev

Check if an input is a valid roman numeral

From Dev

Check if an input is a valid roman numeral

From Dev

Check if user input is a valid date

From Dev

Java - How can i compare chars with other chars?

From Dev

How do I keep asking a user for a valid integer input if there is more than one constraint to the input

From Dev

Python 2.7.10 :Is there any other way to check whether an input is empty other than using len()

From Dev

How do I check if raw input is integer in python 2.7?

From Dev

How can I check if an input is a integer or String, etc.. in JAVA?

From Dev

How to check my input field contains integer value in PHP?

Related Related

  1. 1

    How to check if the input is a valid integer without any other chars?

  2. 2

    How to check if a string is a valid integer?

  3. 3

    How to check if a string is a valid integer?

  4. 4

    How to check the input is an integer or not in Java?

  5. 5

    Check for valid integer

  6. 6

    Check if char array is not garbage? (valid chars)

  7. 7

    How do I make this program check if an input is an integer without causing an error in runtime and stop the loop with a sentinel value?

  8. 8

    How to check if a request is for javascript (or other types) when using Node.js without using any middleware?

  9. 9

    How to make an integer larger than any other integer?

  10. 10

    How do I check an input for an integer in Python?

  11. 11

    How do I check to see if the input is an integer?

  12. 12

    How to check that the entered input is an integer or not in java?

  13. 13

    How can I check for invalid input and loop until the input is valid?

  14. 14

    Is it possible to check if the input is an integer without hasNextLine and exit loop if it's 0?

  15. 15

    Function to check if an integer is valid in PHP

  16. 16

    Check 4 chars with regex Ruby console input

  17. 17

    How to check that none of the chars are in the string?

  18. 18

    Check if input is integer

  19. 19

    Check for input being an integer

  20. 20

    How to check the access token is valid or not without using appId and secret?

  21. 21

    Check if an input is a valid roman numeral

  22. 22

    Check if an input is a valid roman numeral

  23. 23

    Check if user input is a valid date

  24. 24

    Java - How can i compare chars with other chars?

  25. 25

    How do I keep asking a user for a valid integer input if there is more than one constraint to the input

  26. 26

    Python 2.7.10 :Is there any other way to check whether an input is empty other than using len()

  27. 27

    How do I check if raw input is integer in python 2.7?

  28. 28

    How can I check if an input is a integer or String, etc.. in JAVA?

  29. 29

    How to check my input field contains integer value in PHP?

HotTag

Archive