forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarksheet generator
More file actions
176 lines (152 loc) · 8.6 KB
/
Copy pathmarksheet generator
File metadata and controls
176 lines (152 loc) · 8.6 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
#include <iostream> // For input/output operations (cin, cout)
#include <string> // For using std::string (student name, subject names)
#include <vector> // For using std::vector (dynamic arrays for subjects)
#include <iomanip> // For std::fixed, std::setprecision, std::setw (output formatting)
#include <limits> // For std::numeric_limits (input buffer clearing)
#include <fstream> // For file operations (ofstream)
// Function to clear the console screen (platform-dependent)
void clearScreen() {
#ifdef _WIN32
system("cls"); // For Windows
#else
// For Linux/macOS. May not work in all terminals.
// For robust control, consider ncurses or platform-specific APIs.
system("clear");
#endif
}
// Function to calculate the grade based on percentage
std::string calculateGrade(double percentage) {
if (percentage >= 90) return "A+";
else if (percentage >= 80) return "A";
else if (percentage >= 70) return "B+";
else if (percentage >= 60) return "B";
else if (percentage >= 50) return "C+";
else if (percentage >= 40) return "C";
else if (percentage >= 30) return "D+";
else if (percentage >= 20) return "D";
else if (percentage >= 10) return "E+";
else return "E";
}
int main() {
clearScreen(); // Clear screen at the start for a clean interface
std::cout << "\t------------------------------------------\n";
std::cout << "\t Welcome to Marksheet Generator\n";
std::cout << "\t------------------------------------------\n\n";
// --- Student Information Variables ---
std::string studentName;
std::string fatherName;
std::string rollNumber;
const int MIN_SUBJECTS = 2; // Minimum number of subjects required
int numSubjects;
// --- Get Student Details ---
std::cout << "Enter Student Name: ";
std::getline(std::cin, studentName); // Use getline to capture full name with spaces
std::cout << "Enter Father's Name: ";
std::getline(std::cin, fatherName);
std::cout << "Enter Roll Number: ";
std::getline(std::cin, rollNumber);
std::cout << "Enter the number of subjects (minimum " << MIN_SUBJECTS << "): ";
// Input validation loop for number of subjects
while (!(std::cin >> numSubjects) || numSubjects < MIN_SUBJECTS) {
std::cout << "Invalid input. Please enter a number greater than or equal to " << MIN_SUBJECTS << ": ";
std::cin.clear(); // Clear the error flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard invalid input
}
// Consume the leftover newline character after reading numSubjects
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// --- Subject Data Storage ---
std::vector<std::string> subjectNames(numSubjects);
std::vector<double> subjectMarks(numSubjects);
double totalMarks = 0;
const double MAX_MARKS_PER_SUBJECT = 100.0; // Assuming each subject is out of 100
// --- Get Marks for Each Subject ---
std::cout << "\n--- Enter Marks for Each Subject (out of " << static_cast<int>(MAX_MARKS_PER_SUBJECT) << ") ---\n";
for (int i = 0; i < numSubjects; ++i) {
std::cout << "Enter name for Subject " << i + 1 << ": ";
std::getline(std::cin, subjectNames[i]); // Get subject name
double marks;
std::cout << "Enter marks for " << subjectNames[i] << ": ";
// Input validation loop for marks
while (!(std::cin >> marks) || marks < 0 || marks > MAX_MARKS_PER_SUBJECT) {
std::cout << "Invalid marks. Please enter marks between 0 and " << static_cast<int>(MAX_MARKS_PER_SUBJECT) << ": ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
subjectMarks[i] = marks;
totalMarks += marks; // Add marks to total
// Consume the leftover newline character after reading marks
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// --- Calculations ---
double maxPossibleMarks = numSubjects * MAX_MARKS_PER_SUBJECT;
double percentage = (totalMarks / maxPossibleMarks) * 100.0;
// Determine Pass/Fail Status (assuming 40% in each subject is required to pass)
bool passed = true;
const double PASSING_PERCENTAGE_PER_SUBJECT = 40.0;
for (double marks : subjectMarks) {
if (marks < (MAX_MARKS_PER_SUBJECT * (PASSING_PERCENTAGE_PER_SUBJECT / 100.0))) {
passed = false;
break; // Student failed in at least one subject
}
}
// Overall pass/fail also depends on overall percentage (e.g., if overall 50% is required)
// For this example, we'll primarily use per-subject passing for the status,
// and overall percentage for the grade.
std::string finalGrade = calculateGrade(percentage);
// If failed in any subject, overall grade can be adjusted to 'F' regardless of percentage
if (!passed) {
finalGrade = "F";
}
// --- Display Marksheet on Console ---
clearScreen(); // Clear screen before displaying the final marksheet for a clean look
std::cout << "\n\t------------------------------------------\n";
std::cout << "\t FINAL MARKSHEET\n";
std::cout << "\t------------------------------------------\n";
std::cout << std::left << std::setw(20) << "Student Name:" << studentName << std::endl;
std::cout << std::left << std::setw(20) << "Father's Name:" << fatherName << std::endl;
std::cout << std::left << std::setw(20) << "Roll Number:" << rollNumber << std::endl;
std::cout << "\t------------------------------------------\n";
std::cout << std::left << std::setw(25) << "Subject" << std::setw(10) << "Marks" << std::endl;
std::cout << "\t------------------------------------------\n";
for (int i = 0; i < numSubjects; ++i) {
std::cout << std::left << std::setw(25) << subjectNames[i]
<< std::setw(10) << std::fixed << std::setprecision(2) << subjectMarks[i] << std::endl;
}
std::cout << "\t------------------------------------------\n";
std::cout << std::left << std::setw(25) << "Total Marks:" << std::fixed << std::setprecision(2) << totalMarks << " / " << maxPossibleMarks << std::endl;
std::cout << std::left << std::setw(25) << "Percentage:" << std::fixed << std::setprecision(2) << percentage << "%" << std::endl;
std::cout << std::left << std::setw(25) << "Grade:" << finalGrade << std::endl;
std::cout << std::left << std::setw(25) << "Status:" << (passed ? "PASS" : "FAIL") << std::endl;
std::cout << "\t------------------------------------------\n";
// --- Save Marksheet to File ---
std::string filename = rollNumber + "_" + studentName + "_Marksheet.txt";
std::ofstream outFile(filename); // Open a file for writing
if (outFile.is_open()) {
outFile << "------------------------------------------\n";
outFile << " STUDENT MARKSHEET\n";
outFile << "------------------------------------------\n";
outFile << std::left << std::setw(20) << "Student Name:" << studentName << std::endl;
outFile << std::left << std::setw(20) << "Father's Name:" << fatherName << std::endl;
outFile << std::left << std::setw(20) << "Roll Number:" << rollNumber << std::endl;
outFile << "------------------------------------------\n";
outFile << std::left << std::setw(25) << "Subject" << std::setw(10) << "Marks" << std::endl;
outFile << "------------------------------------------\n";
for (int i = 0; i < numSubjects; ++i) {
outFile << std::left << std::setw(25) << subjectNames[i]
<< std::setw(10) << std::fixed << std::setprecision(2) << subjectMarks[i] << std::endl;
}
outFile << "------------------------------------------\n";
outFile << std::left << std::setw(25) << "Total Marks:" << std::fixed << std::setprecision(2) << totalMarks << " / " << maxPossibleMarks << std::endl;
outFile << std::left << std::setw(25) << "Percentage:" << std::fixed << std::setprecision(2) << percentage << "%" << std::endl;
outFile << std::left << std::setw(25) << "Grade:" << finalGrade << std::endl;
outFile << std::left << std::setw(25) << "Status:" << (passed ? "PASS" : "FAIL") << std::endl;
outFile << "------------------------------------------\n";
outFile.close(); // Close the file
std::cout << "\nMarksheet saved to " << filename << std::endl;
} else {
std::cerr << "\nError: Unable to save marksheet to file.\n";
}
std::cout << "\nPress Enter to exit the application.";
std::cin.get(); // Wait for user to press Enter before closing
return 0; // Indicate successful execution
}