Repository navigation
Expand file tree
/
Copy pathUpdated_code.cpp
More file actions
220 lines (212 loc) · 6.88 KB
/
Copy pathUpdated_code.cpp
File metadata and controls
220 lines (212 loc) · 6.88 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
struct Question {
string text;
string topic;
int marks;
string difficulty;
};
class QuestionBank {
private:
vector<Question> questions;
public:
void addQuestion() {
Question q;
cin.ignore();
cout << "\nEnter Question: ";
getline(cin, q.text);
cout << "Enter Topic: ";
getline(cin, q.topic);
cout << "Enter Marks: ";
cin >> q.marks;
cin.ignore();
cout << "Difficulty (Easy/Medium/Hard): ";
getline(cin, q.difficulty);
questions.push_back(q);
saveQuestions();
cout << "\nQuestion Added Successfully!\n";
}
void viewQuestions() {
if (questions.empty()) {
cout << "\nNo Questions Available!\n";
return;
}
cout << "\n========== QUESTION BANK ==========\n";
for (int i = 0; i < questions.size(); i++) {
cout << "\nQuestion " << i + 1 << endl;
cout << "Question : " << questions[i].text << endl;
cout << "Topic : " << questions[i].topic << endl;
cout << "Marks : " << questions[i].marks << endl;
cout << "Difficulty : " << questions[i].difficulty << endl;
}
cout << "\n===================================\n";
}
// Search questions by topic
void searchByTopic() {
if (questions.empty()) {
cout << "\nNo Questions Available!\n";
return;
}
string searchTopic;
cin.ignore();
cout << "\nEnter Topic to Search: ";
getline(cin, searchTopic);
bool found = false;
cout << "\n===== SEARCH RESULTS =====\n";
for (int i = 0; i < questions.size(); i++) {
if (questions[i].topic == searchTopic) {
cout << "\nQuestion: " << questions[i].text << endl;
cout << "Marks: " << questions[i].marks << endl;
cout << "Difficulty: " << questions[i].difficulty << endl;
found = true;
}
}
if (!found) {
cout << "\nNo Questions Found for Topic: "
<< searchTopic << endl;
}
}
// Delete question
void deleteQuestion() {
if (questions.empty()) {
cout << "\nNo Questions Available to Delete!\n";
return;
}
viewQuestions();
int number;
cout << "\nEnter Question Number to Delete: ";
cin >> number;
if (number >= 1 && number <= questions.size()) {
questions.erase(questions.begin() + number - 1);
saveQuestions();
cout << "\nQuestion Deleted Successfully!\n";
}
else {
cout << "\nInvalid Question Number!\n";
}
}
// Generate question paper
void generatePaper(int totalMarks) {
if (questions.empty()) {
cout << "\nNo Questions Available!\n";
return;
}
int collected = 0;
vector<Question> paper;
for (auto q : questions) {
if (collected + q.marks <= totalMarks) {
paper.push_back(q);
collected += q.marks;
}
if (collected == totalMarks)
break;
}
ofstream file("QuestionPaper.txt");
file << "========================================\n";
file << " QUESTION PAPER\n";
file << "========================================\n\n";
file << "Maximum Marks: " << totalMarks << "\n\n";
file << "Instructions:\n";
file << "1. Answer all questions.\n";
file << "2. Marks are indicated against each question.\n\n";
int num = 1;
for (auto &q : paper) {
file << num++ << ") "
<< q.text
<< " ["
<< q.marks
<< " Marks]\n";
file << " Topic: "
<< q.topic
<< " | Difficulty: "
<< q.difficulty
<< "\n\n";
}
file << "========================================\n";
file << "Total Marks: " << collected << endl;
file << "========================================\n";
file.close();
cout << "\nQuestion Paper Generated Successfully!\n";
cout << "File Name: QuestionPaper.txt\n";
if (collected < totalMarks) {
cout << "\nNote: Could only generate "
<< collected
<< " marks out of "
<< totalMarks
<< " marks.\n";
} }
// Save question bank to file
void saveQuestions() {
ofstream file("QuestionBank.txt");
for (auto &q : questions) {
file << q.text << endl;
file << q.topic << endl;
file << q.marks << endl;
file << q.difficulty << endl;
}
file.close();
}
// Load questions when program starts
void loadQuestions() {
ifstream file("QuestionBank.txt");
if (!file) {
return;
}
Question q;
while (getline(file, q.text)) {
getline(file, q.topic);
file >> q.marks;
file.ignore();
getline(file, q.difficulty);
questions.push_back(q);
}
file.close();
}
};
int main() {
QuestionBank qb;
qb.loadQuestions();
int choice;
while (true) {
cout << "\n====================================\n";
cout << " QUESTION PAPER BUILDER\n";
cout << "====================================\n";
cout << "1. Add Question\n";
cout << "2. View All Questions\n";
cout << "3. Search Question by Topic\n";
cout << "4. Delete Question\n";
cout << "5. Generate Question Paper\n";
cout << "6. Exit\n";
cout << "====================================\n";
cout << "Enter Choice: ";
cin >> choice;
if (choice == 1) {
qb.addQuestion();
}
else if (choice == 2) {
qb.viewQuestions();
}
else if (choice == 3) {
qb.searchByTopic();
}
else if (choice == 4) {
qb.deleteQuestion();
}
else if (choice == 5) {
int total;
cout << "\nEnter Total Marks for the Question Paper: ";
cin >> total;
qb.generatePaper(total);
}else if (choice == 6) {
cout << "\nThank You!\n";
cout << "Exiting Question Paper Builder...\n";
break;
}else {
cout << "\nInvalid Input! Try Again.\n";
}
}
return 0;
}