I am trying to get user input and then check if the input is an integer and then return it. If I enter an integer then it will return the value but if the input is a string then it will endlessly output >>>Failed
I was using SO Answer as the base.
#include "stdafx.h"
#include <iostream>
int getRandomNumber(int maxNum) {
return rand() % maxNum + 1;
}
int getIntInput() {
int input;
while (true) {
std::cout << ">>> ";
std::cin >> input;
std::cin.clear();
std::cout << std::flush;
if (std::cin.fail() || input < 0) {
std::cout << "Failed";
continue;
}
break;
}
return input;
}
int main() {
int numGuessed;
int randomNum = getRandomNumber(100);
numGuessed = getIntInput();
}