Hi, I have a quick question to for my programming homework. So what I have to do is see if 3 of these numbers create a triangle, which they do, and it works perfectly.
Though, I want to make it more user friendly. The problem is that if you enter in a letter or a word/sentence instead of a digit, the program just crashes.
My question: How can I make a simple "if" statement there saying that you didn't enter in a digit? I marked where I want to put it.
while( std::cout << "Enter an integer: " && !(cin >> input) ) //if the input failed
{
std::cin.clear(); //clear error flags
std::cin.ignore( std::numeric_limits<std::streamsize>::max() , '\n' ); //ignore all the left over characters in buffer until next line found
std::cout << "Invalid input - Did not enter an integer." << std::endl;
}
One thing to mention the best solution would probably to read as a string then parse. Because this method might have a few bugs if you mix letters and numbers.