mugua_233 2022-10-17 11:36 采纳率: 100%
浏览 26
已结题

一个输出最低、高、总、平均成绩的代码,但是输出都是0

问题遇到的现象和发生背景

想要做一个输出最低、高、总、平均成绩的代码,但是最后输出都是0,感觉应该是函数的int s1那一部分的问题,但是不知道如何更改

用代码块功能插入代码,请勿粘贴截图
#include <iostream>
#include <string>

using namespace std;


int getScore(string);
void findLowestScore(int, int, int, int);
void findHighestScore(int, int, int, int);
void findAverageScore(int, int, int, int);

int main()
{
    int examM, examP, examC, examE;

    for (int i = 0; i < 4; i++)
    {
        switch (i)
        {
        case 0:
            examM = getScore("Maths");
            break;
        case 1:
            examP = getScore("Physics");
            break;
        case 2:
            examC = getScore("Chemistry");
            break;
        case 3:
            examE = getScore("English");
            break;
        }
    }
    findLowestScore(examM, examP, examC, examE);
    findHighestScore(examM, examP, examC, examE);
    findAverageScore(examM, examP, examC, examE);
    return 0;
}

int getScore(string subject)
{
    int scores = 0;
    cout << "Please type in your score for " << subject << ": ";
    cin >> scores;

    while (scores < 0)
    {
        cout << "Invalid input, please re-enter your score!" << endl;
        cin >> scores;
    }
    return 0;
}

void findLowestScore(int s1, int s2, int s3, int s4)
{
    int lowest = 0;
    string subject = "";

    if (s1 < s2 && s1 < s3 && s1 < s4)
    {
        lowest = s1;
        subject = "Maths";
    }
    else if (s2 < s1 && s2 < s3 && s1 < s4)
    {
        lowest = s2;
        subject = "Physics";
    }
    else if (s3 < s1 && s3 < s2 && s3 < s4)
    {
        lowest = s3;
        subject = "Chemistry";
    }
    else
    {
        lowest = s4;
        subject = "English";
    }

    cout << "The lowest score is obtained for " << subject << " with a score of " << lowest << endl;

}

void findHighestScore(int s1, int s2, int s3, int s4)
{
    int highest = 0;
    string subject = "";

    if (s1 > s2 && s1 > s3 && s1 > s4)
    {
        highest = s1;
        subject = "Maths";
    }
    else if (s2 > s1 && s2 > s3 && s1 > s4)
    {
        highest = s2;
        subject = "Physics";
    }
    else if (s3 > s1 && s3 > s2 && s3 > s4)
    {
        highest = s3;
        subject = "Chemistry";
    }
    else
    {
        highest = s4;
        subject = "English";
    }

    cout << "The highest score is obtained for " << subject << " with a score of " << highest << endl;

}

void findAverageScore(int s1, int s2, int s3, int s4)
{
    int total = 0;
    int avg = 0;

    total += s1 + s2 + s3 + s4;
    avg = total / 4;

    cout << "The total is: " << total << endl;
    cout << "The average is: " << avg << endl;
}

  • 写回答

1条回答 默认 最新

  • 快乐鹦鹉 2022-10-17 11:41
    关注

    51行, return 0;改为return scores
    你自己返回了0啊,输入的成绩你不返回

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 10月25日
  • 已采纳回答 10月17日
  • 创建了问题 10月17日

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试