如何改进此GPA计算代码?

时间:2015-09-17 20:41:02

标签: c++

我为一个课程做了这个作业,用户为每个课程输入10个等级,将字母等级转换为数字值(例如" A" = 4)并将其乘以课程和课程将从那里计算GPA。它效果很好,但我想改进这一点,所以当用户输入成绩时,除了" A"," B",&之外,它不会是任何随机值。 #34; C"," D"和" F"。

有什么想法吗?我知道有一个循环,但我不知道从哪里开始。这是我的代码:

 #include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main() {
    //declare variables to store grades, credits, courses and numeric grades
    string letterGrade[10]; //this variable is used to store the user's grades so that we can turn them into numbers
    double numericGrade[10];
    string courses[10] = { "MATH100", "ENG100", "BIO250", "ITM100", "CIS250", "HIST101", "CALC100", "SCI201", "SOC110", "PHY100" };
    int creditForCourses[10] = { 5, 3, 4, 3, 3, 3, 4, 3, 3, 4 };
    double gpa = 0.00;
    int i;

    //this for loop asks the user to enter a grade for each of their courses
    for (i = 0; i < 10; i++) {
        cout << "Enter grade for: " << courses[i] << endl;
        cin >> letterGrade[i];
        }


        //the loop converts the letter grade into a numeric value
    for (i = 0; i < 10; i++) {
        if (letterGrade[i] == "A") {
            numericGrade[i] = 4; //for each if statement, turn a specific letter grade into a value and store it into numericGrade
            }
            else
            if (letterGrade[i] == "B") {
            numericGrade[i] = 3.5;
            }
            else
            if (letterGrade[i] == "C") {
            numericGrade[i] = 3;
            }
            else
            if (letterGrade[i] == "D") {
            numericGrade[i] = 2.5;
            }
            else
            if (letterGrade[i] == "F") {
            numericGrade[i] = 0;
            }
    }





        //calculate gpa by multipling each grade value and each credit from courses
        for (i = 0; i < 10; i++) {
            gpa += (numericGrade[i] * creditForCourses[i]);
        }

        //divide each numeric grade and credit and divide it by 35 (the total amount of credits)
        gpa /= 35;

        //print overall gpa for the student
        cout << "Your GPA is: " << gpa << endl;

    return 0;
}

3 个答案:

答案 0 :(得分:0)

之后
 else
      if (letterGrade[i] == "F") {
          numericGrade[i] = 0;
      }

添加

else {
    cout << "Enter a real grade (A, B, C, D, E, F)" << endl;
    return 1;
}

您可以循环直到他们输入正确的成绩。那是更好的设计,但这是留给读者的一个练习......

答案 1 :(得分:0)

在您的for循环中,要求用户输入每个课程的成绩,您可以限制用户输入有效成绩。这是可能有用的代码。

for (i = 0; i < 10; i++)
{
    cout << "Enter grade for: " << courses[i] << endl;
    char grd;
    cin>>grd;
    if(grd=='A'||grd=='B'||grd=='C'||grd=='D'||grd=='F')
      letterGrade[i] = grd;
    else
    {
        cout<<"Please enter valid grade(A, B, C, D, F)"<<endl;
        i--;
    }
}

答案 2 :(得分:0)

基本上,当输入无效值时,您需要包装要求用户输入条件循环的代码。除了一个小的重构,尝试这样:

for (i = 0; i < 10; i++) {
    cout << "Enter grade for: " << courses[i] << endl;
    for(int done = 0; !done;) {
        cin >> letterGrade[i];

        done = 1; // will be reset back to 0 if no "legal" character is entered

        //the loop converts the letter grade into a numeric value
        if (letterGrade[i] == "A") {
            numericGrade[i] = 4;
        }
        else if (letterGrade[i] == "B") {
            numericGrade[i] = 3.5;
        }
        else if (letterGrade[i] == "C") {
            numericGrade[i] = 3;
        }
        else if (letterGrade[i] == "D") {
            numericGrade[i] = 2.5;
        }
        else if (letterGrade[i] == "F") {
            numericGrade[i] = 0;
        }
        else {
            done = 0;
        }
    }
}