如何创建允许用户输入几个百分比等级然后显示相应字母等级的C ++代码?

时间:2016-10-02 03:56:23

标签: c++ string function iostream

我有这个代码。我需要它允许用户输入五个等级作为百分比,找到它们的平均值,并显示相应的字母等级。它还需要使用我声明的函数。出于某种原因,当程序执行时,即使百分比非常不同,所有字母等级都是相同的。有人可以告诉我,为了显示正确的字母等级,我可能需要编辑一下吗?

#include <iostream>
#include <string>

using namespace std;

float calculateAverage(float math, float english, float science, float history, float geography);
std::string calculateLetterGrade(float grade);

int main()
{

    float math = 0;
    float english = 0;
    float science = 0;
    float history = 0;
    float geography = 0;

    std::cout << "Enter math grade:\n";
    std::cin >> math;
    std::cout << "Enter english grade:\n";
    std::cin >> english;
    std::cout << "Enter science grade:\n";
    std::cin >> science;
    std::cout << "Enter history grade:\n";
    std::cin >> history;
    std::cout << "Enter geography grade:\n";
    std::cin >> geography;

    float sum = (math + english + science + history + geography);
    float Average = sum / 5;

    float grade = (math, english, science, history, geography);
    std::string LetterGrade;

    if (grade >= 90)
    {
    LetterGrade = "A+";
    }
    else if (grade < 90 && grade >= 85)
    {
    LetterGrade = "A";
    }
    else if (grade < 85 && grade >= 80)
    {
    LetterGrade = "A-";
    }
    else if (grade < 80 && grade >= 77)
    {
    LetterGrade = "B+";
    }
    else if (grade < 77 && grade >= 74)
    {
    LetterGrade = "B";
    }
    else if (grade < 74 && grade >= 70)
    {
    LetterGrade = "B-";
    }
    else if (grade < 70 && grade >= 67)
    {
    LetterGrade = "C+";
    }
    else if (grade < 67 && grade >= 64)
    {
    LetterGrade = "C";
    }
    else if (grade < 64 && grade >= 60)
    {
    LetterGrade = "C-";
    }
    else if (grade < 60 && grade >= 57)
    {
    LetterGrade = "D+";
    }
    else if (grade < 57 && grade >= 54)
    {
    LetterGrade = "D";
    }
    else if (grade < 54 && grade >= 50)
    {
    LetterGrade = "D-";
    }
    else
    {
    LetterGrade = "F";
    }

    {
    grade = math;
    std::cout << "Math: " << math << " " << LetterGrade << std::endl;
    }

    {
    grade = english;
    std::cout << "English: " << english << " " << LetterGrade << std::endl;
    }

    {
    grade = science;
    std::cout << "Science: " << science << " " << LetterGrade << std::endl;
    }

    {
    grade = history;
    std::cout << "History: " << history << " " << LetterGrade << std::endl;
    }

    {
    grade = geography;
    std::cout << "Geography: " << geography << " " << LetterGrade << std::endl;
    }

    {
    grade = Average;
    std::cout << "Average: " << Average << " " << LetterGrade << std::endl;
    }

    system("pause");
}

1 个答案:

答案 0 :(得分:0)

float grade = (math, english, science, history, geography);将编译,但它不会做任何有用的事情。更多信息:How does the Comma Operator work

我的建议是实施std::string calculateLetterGrade(float grade);并让它解决繁重问题。

让我们这样做。

std::string calculateLetterGrade(float grade)
{
    std::string LetterGrade;
    if (grade >= 90)
    {
    LetterGrade = "A+";
    }
    else if (grade < 90 && grade >= 85)
    {
    LetterGrade = "A";
    }
    else if (grade < 85 && grade >= 80)
    {
    LetterGrade = "A-";
    }
    else if (grade < 80 && grade >= 77)
    {
    LetterGrade = "B+";
    }
    else if (grade < 77 && grade >= 74)
    {
    LetterGrade = "B";
    }
    else if (grade < 74 && grade >= 70)
    {
    LetterGrade = "B-";
    }
    else if (grade < 70 && grade >= 67)
    {
    LetterGrade = "C+";
    }
    else if (grade < 67 && grade >= 64)
    {
    LetterGrade = "C";
    }
    else if (grade < 64 && grade >= 60)
    {
    LetterGrade = "C-";
    }
    else if (grade < 60 && grade >= 57)
    {
    LetterGrade = "D+";
    }
    else if (grade < 57 && grade >= 54)
    {
    LetterGrade = "D";
    }
    else if (grade < 54 && grade >= 50)
    {
    LetterGrade = "D-";
    }
    else
    {
    LetterGrade = "F";
    }

    return LetterGrade;
}

有更好的方法可以做到这一点,但这是可读的,明显的是发生了什么,目标是什么。它也只是OP首先的剪切和粘贴,所以我没有那种可怕的感觉来自于为他们做别人的功课。

行。我们有一个功能。所以呢。我们用它做什么?

这样的事情:

std::cout << "Math: " << math << " " << calculateLetterGrade(math) << std::endl;
std::cout << "English: " << english << " " << calculateLetterGrade(english) << std::endl;

最重要的是你对C ++语言的理解真的很弱。在课程材料变得更加先进并且你不堪重负之前,你将不得不花费更多的时间来处理问题。现在更难以跑步,而不是稍后冲刺。