这是我的代码:
#include <iostream>
#include <algorithm>
using namespace std;
void getJudgesData(double);
void calcScore(double, double, double, double, double, double);
int findLowest(double, double, double, double, double);
int findHighest(double, double, double, double, double);
double judgeOne, judgeTwo, judgeThree, judgeFour, judgeFive; // global vars
int main() {
cout << "---------------------Star Search---------------------" << endl << endl;
cout << "Enter the 5 scores for the contestant and I will drop the lowest and highest to find the average score:" << endl;
getJudgesData(judgeOne);
getJudgesData(judgeTwo);
getJudgesData(judgeThree);
getJudgesData(judgeFour);
getJudgesData(judgeFive);
cout << "Average score: ";
double outputCalc = 0;
calcScore(judgeOne, judgeTwo, judgeThree, judgeFour, judgeFive, outputCalc);
cout << outputCalc;
}
void getJudgesData(double judgeInput) {
cout << endl << "Enter judge's score: ";
cin >> judgeInput;
while(judgeInput < 0 || judgeInput > 10){
cout << "Please enter a valid score: ";
cin >> judgeInput;
}
}
void calcScore(double inp1, double inp2, double inp3, double inp4, double inp5, double output) {
output = 0;
output = ((inp1 + inp2 + inp3 + inp4 + inp5) - findLowest(inp1, inp2, inp3, inp4, inp5) - findHighest(inp1, inp2, inp3, inp4, inp5)) / 3;
}
int findLowest(double int1, double int2, double int3, double int4, double int5) {
return min({int1, int2, int3, int4, int5});
}
int findHighest(double int1, double int2, double int3, double int4, double int5) {
return max({ int1, int2, int3, int4, int5 });
}
这是作业:
“一个特殊的人才完成工作有五名法官,每个人都会给每个表演者一个0到10分的分数。允许分数分数,例如8.3分。表演者的分数由收到的最高和最低分数决定,然后平均剩余的三个分数。只要用户想为另一个参赛者输入分数,这个过程就可以继续。编写一个使用这种方法来计算参赛者分数的程序。
在您的计划中加入以下功能:
Void getJudgesData()应该询问用户法官的分数,将其存储在参考参数变量中并验证它。对于每个评委,该函数应由main调用一次。 Void calcScore()应计算并显示在删除最高和最低分数后剩余的三个分数的平均值。这个函数应该在main中调用一次,你应该传递5个分数。
calcScore应调用以下两个函数,它使用返回的信息来确定要删除的分数:
int findLowest()应该找到并返回传递给它的五个分数中的最低分。
int findHigest()应该找到并返回传递给它的五个分数中的最高分。“
我无法弄清楚为什么它不会输出正确的值,它编译得很好,但它总是输出零作为平均分数。任何帮助将不胜感激,谢谢!
编辑:该程序现在正常运行!这是代码:
#include <iostream>
#include <algorithm>
using namespace std;
void getJudgesData(double&);
void calcScore(double, double, double, double, double, double&);
int findLowest(double, double, double, double, double);
int findHighest(double, double, double, double, double);
double judgeOne, judgeTwo, judgeThree, judgeFour, judgeFive; // global vars
int main() {
cout << "---------------------Star Search---------------------" << endl << endl;
cout << "Enter the 5 scores for the contestant and I will drop the lowest and highest to find the average score:" << endl;
getJudgesData(judgeOne);
getJudgesData(judgeTwo);
getJudgesData(judgeThree);
getJudgesData(judgeFour);
getJudgesData(judgeFive);
cout << "Average score: ";
double outputCalc = 0;
calcScore(judgeOne, judgeTwo, judgeThree, judgeFour, judgeFive, outputCalc);
cout << outputCalc;
}
void getJudgesData(double& judgeInput) {
cout << endl << "Enter judge's score: ";
cin >> judgeInput;
while(judgeInput < 0 || judgeInput > 10){
cout << "Please enter a valid score: ";
cin >> judgeInput;
}
}
void calcScore(double inp1, double inp2, double inp3, double inp4, double inp5, double& output) {
output = 0;
output = ((inp1 + inp2 + inp3 + inp4 + inp5) - findLowest(inp1, inp2, inp3, inp4, inp5) - findHighest(inp1, inp2, inp3, inp4, inp5)) / 3;
}
int findLowest(double int1, double int2, double int3, double int4, double int5) {
return min({int1, int2, int3, int4, int5});
}
int findHighest(double int1, double int2, double int3, double int4, double int5) {
return max({ int1, int2, int3, int4, int5 });
}
答案 0 :(得分:0)
如果要读取修改后的值,则需要传递引用:
void calcScore(double inp1, double inp2, double inp3, double inp4, double inp5, double& output)
否则output
参数按值传递,当您移出范围时,更改将丢失。
此外,名称选择有点......很奇怪。 double int1
?
答案 1 :(得分:0)
您的程序行为不正确,因为当您将其作为函数的形式参数传递时,double judgeOne会通过值(复制)传递。
在你的情况下,当你传递judOne到getJudgeData()时,一个judOne值的副本将传递给getJudgeData(),并且函数内部发生的任何事情都不会生效。
一种可行的方法:
你应该让getJudgeData()返回类型为double,并像这样分配:
judgeOne = getJudgeData(); //no need for argument