Calculate grade-point average for letter grade - C++ Statement

C++ examples for Statement:switch

Description

Calculate grade-point average for letter grade

Demo Code

                                                  
#include <iostream>
#include <string>
                                                  
class Course {/*  ww  w  .j  a  v a 2  s.  c  o m*/
 private:
    std::string courseName;
    int aCount;
    int bCount;
    int cCount;
    int dCount;
    int fCount;
                                                  
 public:
    explicit Course(std::string);
                                                  
    // SETTERS
    void setCourseName(std::string);
                                                  
    // GETTERS
    std::string getCourseName();
                                                  
    void displayMessage();
    void inputGrades();
    void displayGradeReport();
};
Course::Course(std::string name) {
    setCourseName(name);
                                                  
    aCount = 0;
    bCount = 0;
    cCount = 0;
    dCount = 0;
    fCount = 0;
}
// SETTERS
void Course::setCourseName(std::string name) {
    if (name.length() <= 25) {
        courseName = name;
    } else {
        courseName = name.substr(0, 25);
        std::cout << "Name \"" << name << "\" exceeds maximum length (25).\n"
                  << "Limiting courseName to first 25 characters.\n"
                  << std::endl;
    }
}
// GETTERS
std::string Course::getCourseName() { return courseName; }
void Course::displayMessage() {
    std::cout << "Welcome to the grade book for\n"
              << getCourseName() << "!\n"
              << std::endl;
}
void Course::inputGrades() {
    int grade;
                                                  
    std::cout << "Enter the letter grades.\n"
              << "Enter the EOF character to end input." << std::endl;
                                                  
    while ((grade = std::cin.get()) != EOF) {
        switch (grade) {
            case 'A':
            case 'a':
                ++aCount;
                break;
            case 'B':
            case 'b':
                ++bCount;
                break;
            case 'C':
            case 'c':
                ++cCount;
                break;
            case 'D':
            case 'd':
                ++dCount;
                break;
            case 'F':
            case 'f':
                ++fCount;
                break;
            case '\n':
            case '\t':
            case ' ':
                break;
                                                  
            default:
                std::cout
                    << "Incorrect letter grade entered. Enter a new grade."
                    << std::endl;
        }
    }
}
void Course::displayGradeReport() {
    std::cout << "\n\nNumber of students who received each letter grade:"
              << "\nA: " << aCount << "\nB: " << bCount << "\nC: " << cCount
              << "\nD: " << dCount << "\nF: " << fCount << std::endl;
                                                  
    printf("Grade Point Average: %d\n",
           (aCount * 4 + bCount * 3 + cCount * 2 + dCount * 1 + fCount * 0) /
               (aCount + bCount + cCount + dCount + fCount));
}
                                                  
int main(int argc, const char *argv[]) {
    Course myCourse("Course Name");
                                                  
    myCourse.displayMessage();
    myCourse.inputGrades();
    myCourse.displayGradeReport();
                                                  
    return 0;
}

Result


Related Tutorials