-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathstudentCourseEvaluation.cpp
More file actions
189 lines (171 loc) · 6.31 KB
/
studentCourseEvaluation.cpp
File metadata and controls
189 lines (171 loc) · 6.31 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <string>
using namespace std;
// creating a class named "Student"
void TakeData()
{
// PRECOGS_FIX: perform robust input validation and allow spaces in name using getline
#include <limits>
cout << "ID : ";
if (!(cin >> student_id)) {
cin.clear();
string tmp;
getline(cin, tmp);
cout << "Invalid ID input. Aborting input." << endl;
return;
}
// consume the trailing newline before using getline for full name
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Name: ";
if (!std::getline(cin, student_name) || student_name.empty()) {
cout << "Invalid name input. Aborting input." << endl;
return;
}
auto readValidatedDouble = [&](const string &prompt, double &out, double minVal = 0.0, double maxVal = 1000.0) {
while (true) {
cout << prompt;
if (cin >> out) {
if (out >= minVal && out <= maxVal) {
break;
}
cout << "Value out of accepted range (" << minVal << " - " << maxVal << "). Try again." << endl;
} else {
// PRECOGS_FIX: clear stream and discard invalid token before retry
cin.clear();
string discard;
getline(cin, discard);
cout << "Invalid numeric input. Try again." << endl;
}
}
};
readValidatedDouble("OOP2: ", OOP2_Score, 0.0, 100.0);
readValidatedDouble("Math: ", math_Score, 0.0, 100.0);
readValidatedDouble("English: ", english_Score, 0.0, 100.0);
ctotal();
}
// creating a class named "Employee"
double total_salary()
{
// PRECOGS_FIX: validate inputs and perform multiplication in 64-bit to avoid 32-bit overflow
#include <limits>
long long h = static_cast<long long>(Hours);
long long r = static_cast<long long>(Rate);
// Basic validation: no negative hours/rate allowed for salary computation
if (h < 0 || r < 0) {
return 0.0;
}
const long long maxAllowed = std::numeric_limits<long long>::max();
if (r != 0 && h > maxAllowed / r) {
// Overflow would occur. Cap at max representable or handle as error.
return static_cast<double>(maxAllowed);
}
long long total = h * r;
return static_cast<double>(total);
}
int main()
{
// PRECOGS_FIX: remove unsafe system() use and replace with portable/controlled clear and pause behavior
#include <limits>
auto clearScreen = []() {
// Best-effort portable clear: print several newlines instead of calling system("cls")/system("clear")
for (int i = 0; i < 50; ++i) std::cout << '\n';
};
auto pausePrompt = []() {
std::cout << "Press Enter to continue..." << std::endl;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
};
int choice;
std::cout << "1. Student" << std::endl;
std::cout << "2. Employee" << std::endl;
std::cout << "Your choice: ";
if (!(std::cin >> choice)) {
std::cerr << "Invalid input. Exiting." << std::endl;
return 1;
}
std::cout << std::endl;
Student student; // creating an object of a class "Student"
Employee employee; // creating an object of a class "Employee"
switch (choice)
{
case 1:
{
student.TakeData();
std::cout << std::endl;
student.showData();
}
break;
case 2:
{
while (true)
{
clearScreen(); // PRECOGS_FIX: avoid system("cls") to reduce execution of shell commands
int choice_employee;
std::cout << "1. Input Employee Details\n";
std::cout << "2. Display Employee Details\n";
std::cout << "3. Display Salary\n";
if (!(std::cin >> choice_employee)) {
std::cin.clear();
std::string discard;
std::getline(std::cin, discard);
std::cout << "Invalid selection. Try again." << std::endl;
pausePrompt(); // PRECOGS_FIX: replace system("pause") with controlled prompt
continue;
}
switch (choice_employee)
{
case 1:
{
std::string x, y;
int z, i;
clearScreen();
std::cout << "\t\t\tEmployee information\n";
std::cout << "Employee_ID: ";
if (!(std::cin >> x)) { std::cin.clear(); std::getline(std::cin, x); }
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Employee_Name: ";
std::getline(std::cin, y);
std::cout << "Number of worked Hours: ";
while (!(std::cin >> z)) { std::cin.clear(); std::string discard; std::getline(std::cin, discard); std::cout << "Invalid hours. Enter integer: "; }
std::cout << "Rate per Hour: ";
while (!(std::cin >> i)) { std::cin.clear(); std::string discard; std::getline(std::cin, discard); std::cout << "Invalid rate. Enter integer: "; }
std::cout << std::endl;
employee.setEmployee_ID(x);
employee.setEmployee_Name(y);
employee.setHours(z);
employee.setRate(i);
pausePrompt();
}
break;
case 2:
{
clearScreen();
std::cout << "ID: " << employee.getEmployee_ID() << std::endl;
std::cout << "Name: " << employee.getEmployee_Name() << std::endl;
std::cout << "Hours: " << employee.getHours() << std::endl;
std::cout << "Rate: " << employee.getRate() << std::endl;
pausePrompt();
}
break;
case 3:
{
clearScreen();
std::cout << "Total salary: " << employee.total_salary() << std::endl;
pausePrompt();
}
break;
default:
std::cout << "Incorrect input! Try again!" << std::endl;
pausePrompt();
break;
}
}
}
break;
default:
std::cout << "Incorrect input! Try again!" << std::endl;
break;
}
pausePrompt();
return 0;
}