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
}
More Related Contents:
- Write a c++ function that will prompt a user for an integer and convert it to binary and print in reverse order [duplicate]
- in c++ when i declare an integer variable int a = 200L or int a = 200F or int a = 200U, It allows. How does this happen?
- How to add a number to an integer like a string
- Infinite loop with cin when typing string while a number is expected
- How can I convert a std::string to int?
- C++ convert hex string to signed integer
- How can I use cout
- Testing pointers for validity (C/C++)
- Good input validation loop using cin – C++
- Efficient way to determine number of digits in an integer
- C++ handling very large integers
- Efficient unsigned-to-signed cast avoiding implementation-defined behavior
- Convert String containing several numbers into integers
- How can I validate an integer input [duplicate]
- performance of unsigned vs signed integers
- Why does the most negative int value cause an error about ambiguous function overloads?
- How to handle arbitrarily large integers
- Difference between long and int data types [duplicate]
- Loop until integer input is in required range fails to work with non-digit character inputs
- how to convert from int to char*?
- What is the best way to do input validation in C++ with cin?
- What range of values can integer types store in C++?
- Why use !! when converting int to bool?
- C++ – how to find the length of an integer
- Can I hint the optimizer by giving the range of an integer?
- Long Vs. Int C/C++ – What’s The Point?
- What do the C and C++ standards say about bit-level integer representation and manipulation?
- How Can I avoid char input for an int variable?
- Why it is different between -2147483648 and (int)-2147483648
- Most efficient way to check if all __m128i components are 0 [using