Quick fix
SPOILER: (click to show)
in your code replace
Code:
if (e >= 6 | e <= -1)
{
while (e >= 6 or e <= -1)
{
cout << "The number you've entered isn't a proper choice, please choose 0 through 5." << endl;
cin >> e;
}
}
with
Code:
if (e >= 6 | e <= -1)
{
while (e >= 6 or e <= -1)
{
cout << "The number you've entered isn't a proper choice, please choose 0 through 5." << endl;
cin.clear(); //reset input flags
cin.ignore(numeric_limits<streamsize>::max(),'\n') ;
cin >> e;
}
}
Do this everywhere this problem happens.
Better fix
SPOILER: (click to show)
You could also (if you wish to create extra work) create a function that checks input for letters before returning a value
like so:
Code:
#include <cstdlib>
#include <iostream>
#include <limits>
using namespace std;
int intGet();
int main(int argc, char *argv[])
{
int x = 0;
while (x == 0)
{
x = intGet();
}
cout << endl << "Number:" << x << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
int intGet()
{
int number = 0;
cout << "Enter an integer: ";
cin >> number;
cin.ignore(numeric_limits<int>::max(), '\n');
if (!cin || cin.gcount() != 1)
{
cout << "Please enter a valid integer" << endl;
cin.clear();//reset the input flags
cin.ignore(numeric_limits<int>::max(), '\n');
return 0;
}
else
{return number;}
}
Doing the better fix will give you more control over user input. The quick fix will solve the endless loop problem...but leaves strange results when entering letters.
Example...when the program says "Your name is, right? 1 = yes 2 = no"
when you input "ddd" it will assume that means yes and move on (for whatever the fing reason) . Clearing the Cin will cut out the endless loop but will not control the user input.
p.s. if someone has a better solution post it...I am wet behind the ears on C++