我正在尝试创建一个程序,允许用户输入由五位评委给出的分数。然而,一旦它达到第三个法官的分数,用于标记法官的增量#停止工作,我相信它与if / else语句有关。这是我的代码:
#include <iostream>
#include <string>
using namespace std;
string name;
string city;
double highestScore;
double lowestScore;
double totalScore;
void main()
{
cout << " Report to the media" << endl;
cout << "Enter the diver name: ";
getline(cin, name);
cout << endl;
cout << "Enter the diver's city: ";
getline(cin, city);
cout << endl;
int count = 0;
double score1, score2, score3, score4, score5;
int judgeNum = 0;
judgeNum ++;
cout << "Enter the score given by judge #" << judgeNum << ": ";
cin >> score1;
if (score1 < 0 || score1 > 10)
{
cout << endl;
cout << "Score must be between 1 and 10. Please re-enter score: ";
cin >> score1;
}
cout << endl;
count ++;
while (count <= 5)
{
judgeNum ++;
count ++;
cout << "Enter the score given by judge #" << judgeNum << ": ";
if (judgeNum = 2)
{
cin >> score2;
if (score2 < 0 || score2 > 10)
{
cout << endl;
cout << "Score must be between 1 and 10. Please re-enter score: ";
cin >> score2;
}
}
else
{
if (judgeNum = 3)
{
cin >> score3;
if (score3 < 0 || score3 > 10)
{
cout << endl;
cout << "Score must be between 1 and 10. Please re-enter score: ";
cin >> score3;
}
}
else
{
if (judgeNum = 4)
{
cin >> score4;
if (score4 < 0 || score4 > 10)
{
cout << endl;
cout << "Score must be between 1 and 10. Please re-enter score: ";
cin >> score4;
}
}
else
{
if (judgeNum = 5)
{
cin >> score5;
if (score5 < 0 || score5 > 10)
{
cout << endl;
cout << "Score must be between 1 and 10. Please re-enter score: ";
cin >> score5;
}
}
}
}
}
cout << endl;
}
cout << score1 << score2 << score3 << score4 << score5;
}
答案 0 :(得分:2)
if (judgeNum = 2)
将judgeNum设置为2,然后检查judgeNum的新值是否为“truthy”(非零)。因为新值是2,所以它总是,并且在这里,judgeNum将始终设置为2,然后递增。
在C,C ++和大多数其他语言中,使用两个=
来确定相等性,例如if (judgeNum == 2)
。首先尝试修复它。
答案 1 :(得分:1)
纠正一个
using namespace std;
string name;
string city;
double highestScore;
double lowestScore;
double totalScore;
void main()
{
cout << " Report to the media" << endl;
cout << "Enter the diver name: ";
getline(cin, name);
cout << endl;
cout << "Enter the diver's city: ";
getline(cin, city);
cout << endl;
int count = 0;
double score1, score2, score3, score4, score5;
int judgeNum = 0;
judgeNum ++;
cout << "Enter the score given by judge #" << judgeNum << ": ";
cin >> score1;
if (score1 < 0 || score1 > 10)
{
cout << endl;
cout << "Score must be between 1 and 10. Please re-enter score: ";
cin >> score1;
}
cout << endl;
count ++;
while (count <= 5)
{
judgeNum ++;
count ++;
cout << "Enter the score given by judge #" << judgeNum << ": ";
if (judgeNum == 2)
{
cin >> score2;
if (score2 < 0 || score2 > 10)
{
cout << endl;
cout << "Score must be between 1 and 10. Please re-enter score: ";
cin >> score2;
}
}
else
{
if (judgeNum == 3)
{
cin >> score3;
if (score3 < 0 || score3 > 10)
{
cout << endl;
cout << "Score must be between 1 and 10. Please re-enter score: ";
cin >> score3;
}
}
else
{
if (judgeNum == 4)
{
cin >> score4;
if (score4 < 0 || score4 > 10)
{
cout << endl;
cout << "Score must be between 1 and 10. Please re-enter score: ";
cin >> score4;
}
}
else
{
if (judgeNum == 5)
{
cin >> score5;
if (score5 < 0 || score5 > 10)
{
cout << endl;
cout << "Score must be between 1 and 10. Please re-enter score: ";
cin >> score5;
}
}
}
}
}
cout << endl;
}
cout << score1 << score2 << score3 << score4 << score5;
}