【发布时间】:2015-04-20 00:07:59
【问题描述】:
我在为这个将性别作为循环控制变量的代码编写 while 循环时遇到了麻烦(当您输入性别时,它处于一个 do-while 循环中,具有特定条件使其有效)。另外,我不知道该怎么做才能计算出被录取的候选人的数量和百分比。
char gender; //INPUT Gender of candidate
char genderOk; //CALC & OUT Checks validity of gender input
int height; //INPUT Height of candidate
int heightOk; //CALC & OUT Valid height range for candidate
int weight; //INPUT Weight of candidate
int weightOk; //CALC & OUT Valid weight range for candidates
int count; //INPUT Counter of the FOR loop
int candidates; //INPUT Amount of candidates per test
int acceptCand; //CALC & OUT # of candidates accepted per test
int acceptPerc; //CALC & OUT % of candidates accepted per test
// INPUT - describe input here
cout << left;
for(count = 1; count <= 3; count++)
{
cout << "TEST RUN #" << count << endl << endl << endl;
cout << "Please enter candidate's information (enter 'X' to "
"exit).\n";
gender = 'f';
while(gender != 'X' || gender != 'x')
{
do
{
cout << "Gender: ";
cin.get(gender);
cin.ignore(1000, '\n');
genderOk = gender != 'm' && gender != 'M' && gender != 'f'
&& gender != 'F';
if(genderOk)
{
cout << "***** Invalid gender; please enter M or F "
"*****";
cout << endl;
}
}while(genderOk);
do
{
cout << "Height: ";
cin >> (height);
cin.ignore(1000, '\n');
heightOk = height >= 24 && height <= 110;
if(!heightOk)
{
cout << "***** Invalid height; please enter a height "
"in inches between 24 and 110. *****";
cout << endl;
}
}while(!heightOk);
do
{
cout << "Weight: ";
cin >> weight;
cin.ignore(1000, '\n');
weightOk = weight >= 50 && weight <= 1400;
if(!weightOk)
{
cout << "***** Invalid weight; please enter a weight "
"in lbs between 50 and 1400. *****";
cout << endl;
}
}while(!weightOk);
if(gender == 'm' || gender == 'M')
{
heightOk = height >= 65 && height <= 80;
weightOk = weight >= 130 && weight <= 250;
if(heightOk && weightOk)
{
cout << RESULT << "ACCEPTED!\n\n\n\n";
}
else
{
if(!heightOk)
{
if(!weightOk)
{
cout << RESULT << "rejected based on the "
"HEIGHT and WEIGHT "
"requirements."
"\n\n\n\n";
}
else
{
cout << RESULT << "rejected based on the "
"HEIGHT requirement."
"\n\n\n\n";
}
}
else
{
if(!weightOk)
{
cout << RESULT << "rejected based on the "
"WEIGHT requirement."
"\n\n\n\n";
}
}
}
}
if(gender == 'f' || gender == 'F')
{
heightOk = height >= 62 && height <=75;
weightOk = weight >= 110 && weight <= 185;
if(heightOk && weightOk)
{
cout << RESULT << "ACCEPTED!\n\n\n\n";
}
else
{
if(!heightOk)
{
if(!weightOk)
{
cout << RESULT << "rejected based on the "
"HEIGHT and WEIGHT "
"requirements.\n\n\n\n";
}
else
{
cout << RESULT << "rejected based on the"
" HEIGHT requirement."
"\n\n\n\n";
}
}
else
{
if(!weightOk)
{
cout << RESULT << "rejected based on the "
"WEIGHT requirement."
"\n\n\n\n";
}
}
}
}
}
}
【问题讨论】:
-
什么是“LCV”?你的具体问题是什么?如果您在终止循环时遇到问题,请发布显示特定循环的特定问题的代码(最好是最少的代码)。
-
@Michael - 我认为 Brothermilk 是编程新手,它可能来自一本书(我在那里找到了对 lcv 和哨兵的引用)。我删除了它,因为它对大多数 C/C++/Java/... 人来说并不常见。我希望它不为人所知,而我应该知道它:)
-
您的性别循环前面是打印的消息“X 退出”,但测试未测试“X”...
-
我发现“LCV”代表“循环控制变量”。
-
抱歉不清楚,是的,我是编程新手(但我没有从书中学习“LCV”)。感谢 jonathan leffler 发现我的循环存在问题,但我仍然不知道如何在循环中测试“X”。