C++:程序无法按预期输出

C++: Program won't output as expected

本文关键字:输出 程序 C++      更新时间:2023-10-16

这是我的代码:

#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