char hit; // taking char insted of int because it will get compiler to else statement when somebody types a character
round:// round starts here
{
cin>>hit;
if(hit=='1')
{
hit=10;
}
elseif(hit=='2')
{
hit=5;
}
elseif(hit=='3')
{
hit=7;
}
elseif(hit=='4')
{
hit=12;
}
else
{
cout<<"invalid move... try again";
goto round;
}
} // round ends here
the code runs fine but when i type combinations of keys (like 12,23;1234 etc..) the program changes the value of hitto the sum of all the hit conditions.. like if i type "12" the hit gets assigned to 15..... i want the user to have just one hit per move......
Make hit an int and use cin.fail() to check if the player entered or not a valid value
1 2 3 4 5 6 7
int hit;
cin>>hit;
if(cin.fail()) //if a letter or invalid character was entered instead of a number
{
cout<<"Error:Invalid value entered!"<<endl;//then post a message
goto round;//and go back to the begining
}