do while loops

do
{
cout << "Height: ";
cin >> height;
cin.ignore(1000,'\n');

if (height <= 25 || height >= 110)
{
cout << "***** Invalid height; please enter a "
"height in inches between 24
and 110.*****" << "\n";
}

}while (height <= 25 || height >= 110);

This code works fine when I enter a number, but if a letter is entered the loop will not end. It stays in the loop outputs Height: , then outputs my invalid height statement and then outs Height: , and then invalid statement over and over. I declared height as an int and am not sure what's really happening when a letter is entered. Not quite sure what I need to do to make the loop not run infinitely if a letter is entered. Any suggestions? thanks
This is very incomplete, so it is almost impossible for me to test it myself to see where your problem is going wrong.
You're right sorry about that here's the whole thing.


#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
char gender;
int height;
int weight;
int count;
float acceptedCount;
float userCount;
bool invalid;
bool heightOk;
bool weightOk;

for(count = 1; count <=3; count++)
{
cout << "TEST RUN# " << count << "\n\n";

acceptedCount = 0;
userCount = 0;

do
{
cout << "Please enter candidate's information "
"(enter 'X' to exit)." << "\n";
do
{
cout << "Gender: ";
cin.get(gender);
cin.ignore(1000,'\n');

invalid = (gender != 'M' &&
gender != 'm' &&
gender != 'F' &&
gender != 'f' &&
gender != 'X' &&
gender != 'x');

if(invalid)
{
cout << "***** Invalid gender; please enter M or "
"F *****" << "\n";
}

}while(invalid);

if(gender != 'X' && gender != 'x')
{
userCount = userCount + 1;

do
{
cout << "Height: ";
cin >> height;
cin.ignore(1000,'\n');

if (height <= 25 || height >= 110)
{
cout << "***** Invalid height; please enter a "
"height in inches between 24 and 110. "
"*****" << "\n";
}

}while (height <= 25 || height >= 110);

do
{
cout << "Weight: ";
cin >> weight;
cin.ignore(1000,'\n');

if(weight <= 50 || weight >= 1400)
{
cout << "***** Invalid weight; please enter a "
"weight in lbs between 50 and 1400. "
"*****" << "\n";
}

}while(weight <= 50 || weight >= 1400);


heightOk = ((gender == 'M' ||
gender == 'm') &&
height >= 65 &&
height <= 80) ||
((gender == 'F' ||
gender == 'f') &&
height >= 62 &&
height <= 75);

weightOk = ((gender == 'M'||
gender == 'm') &&
weight >= 130 &&
weight <= 250) ||
((gender == 'F' ||
gender == 'f') &&
weight >= 110 &&
weight <= 185);

if(heightOk && weightOk)
{
cout << "\nThe candidate has been ACCEPTED!" << "\n\n\n";
acceptedCount = acceptedCount + 1;
}
else if (heightOk && !weightOk)
{
cout << "\nThe candidate has been rejected based on WEIGHT"
" requirement." << "\n\n\n";
}
else if (weightOk && !heightOk)
{
cout << "\nThe candidate has been rejected based on the "
"HEIGHT requirement." << "\n\n\n";
}
else
{
cout << "\nThe candidate has been rejected based on the"
" HEIGHT and WEIGHT requirement." << "\n\n\n";
}
}

}while(gender != 'X' && gender != 'x');

if(userCount > 0)
{
cout << "\n";
cout << acceptedCount << " candidate(s) accepted!" << "\n";
cout << setprecision(2);
cout << "That's " << (acceptedCount / userCount) * 100 << "%!" <<
"\n\n\n";
}
}

return 0;
}
Try this:

1
2
3
4
5
...
cin >> height;
cin.clear();
cin.ignore(1000,'\n');
...


and the same for the weight
Thanks rich that worked! I had never heard of that, I was thinking that it had something to do with enter a letter into data type int but didn't know what to do. Thanks again!
Topic archived. No new replies allowed.