Here is the code
double enter_number()
{
double number;
while(1)
{
cin>>number;
if(cin.fail())
{
cin.clear()
I would prefer the following code. Among many codes to clear this question out of our minds, I think this is most appropriate code I found online.
#include
#include
using namespace std;
int main(){
int n;
while(!(cin >> n)) {
cin.clear(); // to clear the buffer memory
cin.ignore(numeric_limits::max(), '\n');
cout << "Please enter a valid integer!";
}
cout << "Your Integer: " << n << endl;
return 0;
}