程序结束太早

时间:2019-03-14 20:24:27

标签: c++

我正在编写一个程序,其中5名法官输入0到10之间的一个分数,并在减去最高和最低分数之后计算平均值。每次我运行该程序时,它只计算一个分数便会计算平均值,然后停止该程序。我不知道我在做什么错。我如何才能使其在收集所有5个分数,降低最高和最低分数,然后计算平均值的地方运行?这是我的代码:

#include "pch.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


void getjudgescore(double& judge1, double& judge2, double& judge3, double& judge4, double& judge5)
{
    for (int judge = 1; judge < 6; judge++)
    {
        double j = 0;
        do
        {
            cout << "Judge #" << judge << " - Please enter a score between 0.0 and 10.0 : ";
            cin >> j;
            if (j < 0 || j > 10)
            {
                cout << "Score must be between 0.0 and 10.0 : ";
                cin >> j;
            }
        } while (j < 0 || j > 10);
        switch (judge)
        {
        case 0: if (judge == 1)
        {
            judge1 = j;
            break;
        }
        case 1: if (judge == 2)
        {
            judge2 = j;
            break;
        }
        case 2: if (judge == 3)
        {
            judge3 = j;
            break;
        }
        case 3: if (judge == 4)
        {
            judge4 = j;
            break;
        }
        case 4: if (judge = 5)
        {
            judge5 = j;
            break;
        }
        }
    }
}
double findLowest(double& judge1, double& judge2, double& judge3, double& judge4, double& judge5)
{
    double lowest = 10;
    if (lowest > judge1)
    {
        lowest = judge1;
    }
    if (lowest > judge2)
    {
        lowest = judge2;
    }
    if (lowest > judge3)
    {
        lowest = judge3;
    }
    if (lowest > judge4)
    {
        lowest = judge4;
    }
    if (lowest = judge5)
    {
        lowest = judge5;
    }
    return lowest;
}
double findHighest(double& judge1, double& judge2, double& judge3, double& judge4, double& judge5)
{
    double highest = 0;
    if (judge1 > highest)
    {
        highest = judge1;
    }
    if (judge2 > highest)
    {
        highest = judge2;
    }
    if (judge3 > highest)
    {
        highest = judge3;
    }
    if (judge4 > highest)
    {
        highest = judge5;
    }
    return highest;
}
double calcScore(double& judge1, double& judge2, double& judge3, double& judge4, double& judge5, double& highest, double& lowest)
{
    double total = 0;
    double average = 0;
    total = judge1 + judge2 + judge3 + judge4 + judge5;
    total = total - highest - lowest;
    average = total / 3;
    cout << "Final Score : " << average;
    return average;
}
int main()
{
    double judge1 = 0, judge2 = 0, judge3 = 0, judge4 = 0, judge5 = 0;
    double totalscore = 0;
    double highscore = 0;
    double lowscore = 0;
    double highest = 0;
    double lowest = 0;
    getjudgescore(judge1, judge2, judge3, judge4, judge5);
    findLowest(judge1, judge2, judge3, judge4, judge5);
    findLowest(judge1, judge2, judge3, judge4, judge5);
    calcScore(judge1, judge2, judge3, judge4, judge5, highest, lowest);
}

1 个答案:

答案 0 :(得分:-1)

这是您开关中的if语句,它们全都偏移了一个。

switch(judge)
{
    case 0: if (judge == 1)
    {
        judge1 = j;
        break;
    }

if永远不会评估为true,因为大小写有效时judge为0。

此外,开关中的if语句是多余的。