if numInput isnt an integer I want it to prompt "Invalid Try Again" and ask
for input again
std::cout << "Enter a Number (Max 10 digits)(99 to Exit): ";
std::cin >> numInput; Reading a whole input line and validating the text is usually the simplest, most predictable way to loop until a valid integer is provided. 's double-based test and 's correct stream-clear pattern both illustrate problems and fixes when using operator>>; another robust option is to parse a trimmed line (so the code never has to juggle failbits or leftover characters).
Example (C++17+): read a line, trim whitespace, parse with std::from_chars, ensure the entire token was consumed, and enforce the "max 10 digits" and the sentinel 99.
#include <iostream>
#include <string>
#include <charconv>
int main() {
std::string line;
while (std::cout << "Enter a Number (Max 10 digits)(99 to Exit): ",
std::getline(std::cin, line)) {
// trim (simple)
auto first = line.find_first_not_of(" \t\r\n");
if (first == std::string::npos) { std::cerr << "Invalid Try Again\n"; continue; }
auto last = line.find_last_not_of(" \t\r\n");
std::string token = line.substr(first, last - first + 1);
long long value = 0;
auto res = std::from_chars(token.data(), token.data() + token.size(), value);
if (res.ec != std::errc() || res.ptr != token.data() + token.size()) {
std::cerr << "Invalid Try Again\n";
continue;
}
// max 10 digits (ignore optional sign)
std::string digits = token;
if (!digits.empty() && (digits[0] == '+' || digits[0] == '-')) digits.erase(0,1);
if (digits.size() > 10) { std::cerr << "Too many digits\n"; continue; }
if (value == 99) break;
std::cout << "You entered: " << value << '\n';
}
} Notes: std::from_chars is non-throwing and reports exactly where parsing stopped; it does not skip whitespace, so explicit trimming matters. If the toolchain lacks C++17 from_chars, use std::strtol (check end-pointer) or std::stringstream/std::stoi with care (they can throw). See the reference pages for details: std::from_chars and std::getline.
Jump to Post— rxlim 2Have a look at this http://www.daniweb.com/forums/post1476820.html#post1476820 thread
Here's an example based on your request:
#include <iostream>
int main(){
double numInput;
while(true){
std::cout << "Enter a Number (Max 10 digits)(99 to Exit): ";
std::cin >> numInput;
if( numInput - static_cast<int>(numInput) != 0.0 )
std::cout << "Invalid Try Again" << std::endl;
else
break;
}
if( numInput == 99.0 )
return 0;
std::cout << "You entered: " << static_cast<int>(numInput);
return 0;
} Here's an example based on your request:
#include <iostream> int main(){ double numInput; while(true){ std::cout << "Enter a Number (Max 10 digits)(99 to Exit): "; std::cin >> numInput; if( numInput - static_cast<int>(numInput) != 0.0 ) std::cout << "Invalid Try Again" << std::endl; else break; } if( numInput == 99.0 ) return 0; std::cout << "You entered: " << static_cast<int>(numInput); return 0; }
Why if you type "g"? That's not an integer either, but it throws your program into an infinite loop because cin is put into an error state. The usual pattern for validating input works like so:
#include <iostream>
#include <ios>
#include <limits>
int main()
{
int value;
while (std::cout<<"Please enter an integer: ", !(std::cin>> value)) {
// Clear the stream state so we can flush
std::cin.clear();
// Flush all input on the current line
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// Notify the user of an error
std::cerr<<"Invalid input. ";
}
std::cout<<"You entered "<< value <<'\n';
} Of course, stream input isn't quite as easy as we would sometimes like. 123.456 will succeed and place 123 in value while leaving ".456" in the stream. To protect against this kind of error (if it's an error in your app), you also need to peek the next character and verify that it's either a newline or end-of-file (optionally trimming trailing whitespace if you feel especially persnickety):
#include <iostream>
#include <ios>
#include <limits>
#include <cctype>
bool valid_state(std::istream& in)
{
if (!in)
return false;
else {
std::istream::int_type c;
while ((c = in.peek()) != '\n' && std::isspace(c)) {
// Trim a non-newline whitespace character
in.get();
}
return c == '\n' || c == EOF;
}
}
int main()
{
int value;
while (std::cout<<"Please enter an integer: ", !valid_state(std::cin>> value)) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cerr<<"Invalid input. ";
}
std::cout<<"You entered "<< value <<'\n';
} We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.