How can I repeatedly ask the user until "cin" a satisfactory input?
Case 1: until "cin" 1-100, other letters and notes are not allowed
Case 2: until "cin" positive integer, negative numbers, 0, other letters and notes are not allowed
while(1){
//BFH:
cout << " Pre-Game Menu:" << endl;
char c[1000]; //c[1] = NULL; //debugging
c[1] = 0;
cout << endl;
cout << " Enter 1 for option 1. " << endl;
cout << " Enter 2 for option 2. " << endl;
cout << " Type 'return' to return to proceeding menu. " << endl;
cout << " Type 'menu' to return to the main menu. " << endl;
cout << " Type 'Exit' to exit. " << endl;
enter:
printf("%s"," >");
string templine;
templine = getstring();
assign(templine, c);
if (stringigual("exit", c) ) exit(0);
if (stringigual("menu", c) )
{cout << "You are already at the main menu! " << endl;}
if (stringigual("return", c) )
{break;} //use this if this is in a while loop structure, use return; if in a function etc...
if (c[1] == 0)
{
choice = c[0] - 48; //}
if ((choice < 1) || (choice > 2))
{ goto enter; }
}
if(!isdigit(c[0]) ) goto enter; menuswitch(choice);
}//end while
you can easily change it to fit your needs.
Still not perfect however, I've noticed upon further iterations there is some sort of leftover junk from the cin that is being inserted into my variables by mistake. so i tried using cin.ignore but ended up with the following functions: