In this tutorial, we introduce a C++ program to calculate CGPA and GPA: a GPA/CGPA Calculator C++. This guide enhances your understanding of C++ programming by applying basic concepts to create a functional application. Follow along as we break down the process of implementing this calculator, which will bolster your C++ skills and provide a useful tool for academic assessment.
Project Overview, C++ GPA/CGPA Calculator C++ Code
The GPA calculator project revolves around creating a console application in C++ that calculates the GPA (Grade Point Average) and CGPA (Cumulative Grade Point Average) based on user input. This includes capturing the number of subjects or semesters, their respective credit hours, and grades, then applying the calculation to produce a result.


Code Overview
The application is structured around several key functions:
InputValidation:
Ensures all user input is valid, prompting re-entry if necessary.
Calculate GPA:
Calculates the GPA in the GPA calculator based on input grades and credit hours.
Calculate CGPA:
Calculates the CGPA in the CGPA calculator from multiple GPA values.
main function:
The application’s entry point is where the user’s journey begins.
Here’s a simplified version of what the main components look like:
C++ GPA Calculator Code
#include <iostream>
#include <vector>
#include <limits>
#include <iomanip>
#include <sstream>
using namespace std;
// Function Prototypes
void clearScreen();
void printHeader(const string& title);
void calculateGPA();
void calculateCGPA();
void displayMethod();
void exitApplication();
float inputValidation(const string& prompt);
int main() {
while (true) {
clearScreen();
printHeader("GPA & CGPA Calculator");
cout << "1. Calculate GPA\n";
cout << "2. Calculate CGPA\n";
cout << "3. How GPA & CGPA are calculated\n";
cout << "4. Exit\n";
cout << "\nEnter your choice: ";
int choice = static_cast<int>(inputValidation(""));
switch (choice) {
case 1:
calculateGPA();
break;
case 2:
calculateCGPA();
break;
case 3:
displayMethod();
break;
case 4:
exitApplication();
return 0;
default:
cout << "Invalid choice. Please enter a valid option.\n";
break;
}
cout << "\nPress Enter to continue...";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get(); // Wait for user to press enter
}
}
void clearScreen() {
// Clear the screen and move cursor to top-left corner
cout << "\033[2J\033[1;1H";
}
void printHeader(const string& title) {
cout << setw(50) << setfill('-') << "\n";
cout << setw((50 + title.length()) / 2) << setfill(' ') << title << "\n";
cout << setw(50) << setfill('-') << "\n\n";
}
void calculateGPA() {
clearScreen();
printHeader("GPA Calculation");
int numSubjects = static_cast<int>(inputValidation("Enter the number of subjects: "));
vector<float> credits(numSubjects), grades(numSubjects);
float totalCredits = 0, weightedSum = 0;
for (int i = 0; i < numSubjects; ++i) {
stringstream ss;
ss << "Enter credit hours for subject " << i + 1 << ": ";
credits[i] = inputValidation(ss.str());
ss.str(""); // Clear the stringstream
ss.clear(); // Clear any flags
ss << "Enter grade (point) for subject " << i + 1 << ": ";
grades[i] = inputValidation(ss.str());
weightedSum += credits[i] * grades[i];
totalCredits += credits[i];
}
cout << "Your GPA is: " << setprecision(2) << fixed << weightedSum / totalCredits << endl;
}
void calculateCGPA() {
clearScreen();
printHeader("CGPA Calculation");
int numSemesters = static_cast<int>(inputValidation("Enter the number of semesters: "));
float sumGPA = 0;
for (int i = 0; i < numSemesters; ++i) {
stringstream ss;
ss << "Enter GPA for semester " << i + 1 << ": ";
sumGPA += inputValidation(ss.str());
}
cout << "Your CGPA is: " << setprecision(2) << fixed << sumGPA / numSemesters << endl;
}
void displayMethod() {
clearScreen();
printHeader("Calculation Method");
cout << "GPA is calculated as the sum of (credit hours * grade points) for all subjects, divided by the total credit hours.\n";
cout << "CGPA is the average of GPA calculated across all semesters.\n\n";
}
void exitApplication() {
clearScreen();
cout << "Thank you for using the GPA & CGPA Calculator. Goodbye!\n";
}
float inputValidation(const string& prompt) {
float input;
if (!prompt.empty()) cout << prompt;
while (!(cin >> input) || input < 0) {
cout << "Invalid input. Please enter a positive number: ";
cin.clear(); // Clear input buffer
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Discard input
}
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Clear the rest of the line
return input;
}
GitHub Repository
Below is the to our GitHub Repository, where you can directly clone the C++ GPA Calculator Code Project and start your coding by clicking on the button below.
Here You can get the full source code from our GitHub Repository by cloning it.
How It Works
Input Validation
This function ensures that all numeric input is positive and valid, preventing errors in the calculation in the GPA/CGPA Calculator C++ project due to incorrect data.
Calculating GPA
The `calculate GPA` function prompts the user to enter grades and credit hours for each subject, then calculates the GPA as the weighted sum of grades divided by the total credit hours.
Calculating CGPA
Similarly, `calculate GPA` gathers GPA values for each semester and computes the average to find the CGPA.
Running the Application
Compile and run your application through your IDE or command line. You’ll be greeted with options to calculate either GPA or CGPA, along with instructions for entering your data.
Conclusion
By following these steps, you’ve created a functional GPA and CGPA calculator project in C++. This project not only serves an educational purpose but also provides practical utility for students and educators. Through the process, you’ve practiced essential C++ programming concepts such as input validation, working with vectors, and implementing basic arithmetic operations in a real-world application.
FAQ
Can I expand this project?
Absolutely! Consider adding features like saving results to a file, or incorporating a graphical user interface (GUI) with libraries such as Qt or SFML for a more user-friendly experience.
How can I ensure accuracy in the calculations?
Double-check the logic in your `calculateGPA` and `calculateCGPA` functions. You can also write unit tests to automatically verify that your calculations are correct for various input scenarios.