Ok, my problem is a rather simple one (I think) but can't seem to find what I need online. I am writing a fitness program. The function I am having trouble with asks the user to enter 1 or 2 for male or female. I have it set up so if they enter >2 or <1 that it will simply loop, clear screen and ask question again. This part works fine, the problem is if the user enters "m" accidently the program freaks out and continues looping. How can I prevent the user from entering anything but 1 or 2. Is there a work-around for this problem?
Code:
// Assess your expenditure
// Ask user if male or female and calculate RMR dependant on given formula
// Store variables to be used in Step4
int Step2()
{
cout << "Enter your sex:\n\n";
cout << " [1] Male\n";
cout << " [2] Female\n\n";
cout << "Enter selection: ";
cin >> Sex;
if (Sex == 1 || Sex == 2)
{
if (Sex == 1)
{
system("CLS");
cout << "Enter your weight: ";
cin >> Weight;
cout << "\nEnter your Height (in inches): ";
cin >> Height;
cout << "\nEnter your age: ";
cin >> Age;
RMR = 66 + (13.7 * (Weight / 2.2)) + (5 * (Height * 2.54)) - (6.8 * Age);
return 0;
}
if (Sex == 2)
{
system("CLS");
cout << "Enter your weight: ";
cin >> Weight;
cout << "\nEnter your Height (in inches): ";
cin >> Height;
cout << "\nEnter your age: ";
cin >> Age;
RMR = 655 + (9.6 * (Weight / 2.2)) + (1.8 * (Height * 2.54)) - (4.7 * Age);
return 0;
}
}
else
{
system("CLS");
Step2();
return 0;
}
}