C++:程序无法按预期输出
C++: Program won't output as expected
这是我的代码:
#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()应该计算并显示掉最高和最低分数后剩下的三个分数的平均值。这个函数应该被调用一次,你应该给它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 });
}
您的程序行为不正确,因为当您将double judgeOne作为函数的形式参数传递时,它是按值传递的(复制的)。
在您的情况下,当您将judgeOne传递给getJudgeData()时,judgeOne值的副本将传递给getJusticeData(),函数内部发生的任何事情都不会生效。
一种行之有效的方法:
您应该让getJudgeData()的返回类型为double,并像这样赋值:
judgeOne = getJudgeData(); //no need for argument
如果您想读取修改后的值,则需要传递引用:
void calcScore(double inp1, double inp2, double inp3, double inp4, double inp5, double& output)
否则,output
参数将按值传递,并且在移出范围时更改将丢失。
此外,名称选择有点。。。奇怪的double int1
?
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 显示错误输出的简单数组排序程序
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 如何从Windows应用程序输出到标准?
- 我写了一个C++程序来模拟Enigma机器.我没有得到输出
- 如何使用C++读取另一个程序中的源代码输出
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 为什么这个程序的输出不如预期
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- C++ 每次运行程序时我都会"nan"输出的问题
- 如何从 Win32 C++ 应用程序输出到父控制台窗口?
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 而循环:简单的除法程序输出零,不明白为什么
- 如何在 html 页面中插入 cgi 应用程序的输出?
- C 中的输出程序无明显的原因是白色的
- C++:更改简单输出程序的字体
- C++ 简单的输入/输出程序 + 枚举器
- C++字母等效数字代码输出程序
- 编译输出程序集