To create a simple GPA (Grade Point Average) calculator in C++, you need to take input for grades and credit hours for multiple courses, calculate the GPA for each course, and then compute the overall GPA. Here's a basic example of a C++ program to do this:

Source code


#include <iostream>

#include <vector>


struct Course {

    std::string name;

    double grade;

    double creditHours;

};


int main() {

    int numCourses;

    std::cout << "Enter the number of courses: ";

    std::cin >> numCourses;


    std::vector<Course> courses;


    for (int i = 0; i < numCourses; i++) {

        Course course;

        std::cout << "Enter the name of course " << i + 1 << ": ";

        std::cin >> course.name;

        std::cout << "Enter the grade for " << course.name << " (in GPA scale): ";

        std::cin >> course.grade;

        std::cout << "Enter the credit hours for " << course.name << ": ";

        std::cin >> course.creditHours;

        courses.push_back(course);

    }


    double totalGradePoints = 0.0;

    double totalCreditHours = 0.0;


    for (const Course& course : courses) {

        totalGradePoints += course.grade * course.creditHours;

        totalCreditHours += course.creditHours;

    }


    if (totalCreditHours > 0) {

        double gpa = totalGradePoints / totalCreditHours;

        std::cout << "Your GPA is: " << gpa << std::endl;

    } else {

        std::cout << "No credit hours entered. GPA cannot be calculated." << std::endl;

    }


    return 0;

}


Output



In this program, the user is prompted to enter the number of courses, and then they input the course name, grade in GPA scale, and credit hours for each course. The program calculates the GPA for each course and then computes the overall GPA based on the entered data.


Please note that this is a simple GPA calculator and doesn't account for variations in grading scales or other factors that can affect GPA calculations in real-world scenarios.

Next Post Previous Post
No Comment
Add Comment
comment url