dimanche 4 octobre 2015

A looping issue when a non integar is inputed

I am very new to C++ programming. Could someone show me what I'm doing wrong so that when non integers are entered in 'num1', an infinite loop of the following if statement.

//Nummber Guessing Game
#include <iostream>
#include <ctime>

using namespace std;

void printSomething()
{
cout<<"Guess the number between 1 and 20!\n";
}
int main()

{
printSomething();
srand((signed)time(0));
int number = rand()%20+1;
int num1 = 0;

stop: 
num1= 100;
cin>>num1;
while(num1 != number)
{
cin >> num1;
if  (!cin){cout<< "Only numbers\n"; 
goto stop; }
if (num1 < number){cout<< "Too Low\n";}
if (num1 > number){cout<< "Too High\n";}

}
if (num1 = number){ cout<< "Thats right!! you figured it out!\n"; }

cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

return 0;
}

Aucun commentaire:

Enregistrer un commentaire