-
Notifications
You must be signed in to change notification settings - Fork 0
/
CGPA Calculator.cpp
108 lines (91 loc) · 3.2 KB
/
CGPA Calculator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <iomanip>
#include <cstdlib> // Include this header for exit()
using namespace std;
class Student {
private:
string _name;
string _rollNo;
int _semester;
public:
void setStudentData() {
cout << "Enter your name: ";
cin.ignore(); // Clear input buffer
getline(cin, _name);
cout << "Enter your University Roll-No: ";
getline(cin, _rollNo);
cout << "Enter your current semester: ";
cin >> _semester;
}
void printStudentData() {
cout << "\n----------Result Card-----------\n";
cout << "\nStudent name: " << _name << endl;
cout << "Student BZU Roll-No: " << _rollNo << endl;
cout << "Semester: " << _semester << endl;
}
};
class Cgpa {
private:
int _totalSubjects;
float _subjectGPA;
double _totalGPA = 0.0;
public:
void getMarks() {
cout << "Enter your total subjects: ";
cin >> _totalSubjects;
for (int i = 1; i <= _totalSubjects; ++i) {
do {
cout << "Enter GPA for subject " << i << ": ";
cin >> _subjectGPA;
// Validate GPA (assuming it's within the range [0, 4])
if (_subjectGPA < 0.0 || _subjectGPA > 4.0) {
cout << "Invalid GPA. Please enter a value between 0 and 4." << endl;
}
} while (_subjectGPA < 0.0 || _subjectGPA > 4.0);
_totalGPA += _subjectGPA; // Add valid GPA to the total
}
}
void calculateCGPA() {
double totalGP = _totalGPA / _totalSubjects;
cout << "Total Obtained GPA: " << _totalGPA << endl;
cout << fixed << setprecision(2); // Precision to 2 decimal places
cout << "CGPA = " << totalGP << "\n";
cout << "\nCalculated Successfully\n";
}
};
int main() {
cout << "+------------------------------------------------------+\n";
cout << "' '\n";
cout << "' CGPA-Calculator '\n";
cout << "' '\n";
cout << "+------------------------------------------------------+\n\n";
while (true) {
int choice;
cout << "\nMENU : " << endl;
cout << "1. Enter Application ?" << endl;
cout << "2. Exit Application ?" << endl;
cout << "Enter your choice : ";
cin >> choice;
switch (choice) {
case 1: {
system("cls");
cout << "Calculating....\n";
Student obj1;
obj1.setStudentData();
Cgpa obj2;
obj2.getMarks();
system("cls");
obj1.printStudentData();
obj2.calculateCGPA();
break;
}
case 2:
system("cls");
cout << "Exiting the application...." << endl;
exit(EXIT_SUCCESS); // Exit the program
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}