-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuizCraft.cpp
More file actions
457 lines (404 loc) · 13.1 KB
/
QuizCraft.cpp
File metadata and controls
457 lines (404 loc) · 13.1 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include <iostream>
#include <fstream>
#include <sstream>
#include<string>
#include <vector>
#include <set>
#include <regex>
#include <algorithm>
using namespace std;
string currentUser;
void checkLibraryIncludes(const string& code, vector< string>& errors);
void checkSemicolons(const string& code, vector< string>& errors);
void checkVariableNames(const string& code, vector< string>& errors);
class Leaderboard {
private:
vector<pair<string, int>> scores;
string currentUser;
public:
void writeScoresToFile() const;
void display() const;
void addScore(const string& user, int score);
void saveToFile() const;
void loadFromFile();
int getUserScore(const string& user) const;
};
void Leaderboard::writeScoresToFile() const {
fstream file("leaderboard.txt", ios::out);
if (!file.is_open()) {
cout << "Error opening file for writing leaderboard!" << endl;
return;
}
for (const auto& entry : scores) {
file << entry.first << " " << entry.second << endl;
}
file.close();
}
void Leaderboard::display() const {
cout << "\t\t\t Leaderboard \n";
cout << "\t\t\t ------------ \n";
for (const auto& entry : scores) {
if (entry.first == currentUser) {
cout << "* User: " << entry.first << ", Score: " << entry.second << " *" << endl; // Highlight current user
} else {
cout << "User: " << entry.first << ", Score: " << entry.second << endl;
}
}
}
void Leaderboard::addScore(const string& user, int score) {
bool userFound = false;
for (auto& entry : scores) {
if (entry.first == user) {
entry.second += score;
userFound = true;
break;
}
}
if (!userFound) {
scores.push_back(make_pair(user, score));
}
cout << endl << "Added Score: User=" << user << ", Score= " << score << endl;
}
void Leaderboard::saveToFile() const {
writeScoresToFile();
}
void Leaderboard::loadFromFile() {
fstream file("leaderboard.txt", ios::in);
if (!file.is_open()) {
cout << "Error opening leaderboard file!" << endl;
return;
}
scores.clear();
string user;
int score;
while (file >> user >> score) {
scores.push_back(make_pair(user, score));
}
file.close();
}
int Leaderboard::getUserScore(const string& user) const {
for (const auto& entry : scores) {
if (entry.first == user) {
return entry.second;
}
}
return 0; // Default score if user is not found
}
Leaderboard leaderboard;
class User {
public:
string userID, password;
void registerUser() {
system("cls");
cout << "\t\t\t Please Register Yourself: "<<endl;
cout << "\t\t\t USERNAME: ";
cin >> userID;
cout << "\t\t\t PASSWORD: ";
cin >> password;
ofstream f1("records.txt", ios::app);
f1 << userID << ' ' << password << endl;
system("cls");
f1.close();
cout << "\t\t\t Successfully Registered!\n";
loginUser();
}
bool loginUser() {
string id, pass;
system("cls");
cout << "\t\t\t Please Login Your Account"<<endl;;
cout << "\t\t\t USERNAME: ";
cin >> userID;
cout << "\t\t\t PASSWORD: ";
cin >> password;
ifstream input("records.txt");
while (input >> id >> pass) {
if (userID == id && password == pass) {
currentUser = id;
input.close();
system("cls");
cout << "\t\t\t Successfully Logged In! \n\n";
return true;
}
}
input.close();
cout << "\t\t\t INVALID USERNAME OR PASSWORD! \n\n";
return false;
}
};
class Quiz {
public:
char quizLevel;
void startQuiz() {
cout << "\t| Press B for Beginner level |\n";
cout << "\t| Press I for Intermediate level |\n";
cout << "\t| Press A for Advanced level |\n";
cout << "\t\t\t Enter Your Choice : ";
cin >> quizLevel;
switch (quizLevel) {
case 'B':
case 'b': {
beginner();
break;
}
case 'I':
case 'i': {
intermediate();
break;
}
case 'A':
case 'a': {
advance();
break;
}
default: {
system("cls");
cout << "\t\t\t Please select a valid option: \n\n";
startQuiz();
}
}
}
private:
struct Question {
string question;
string answer;
};
void beginner() {
quizLevel= 'B';
const int numQuestions = 5;
Question questions[numQuestions] = {
{"How many Types of Programming Language are there?", "Two"},
{"Name any one type of Programming Language.", "High level or Low level"},
{"C++ is Developed by?", "Bjarne"},
{"C++ is Developed at?", "Bell lab"},
{"C++ is Developed in?", "1980"}
};
int indexes[numQuestions] = {0, 1, 2, 3, 4};
srand(time(0));
for (int i = 0; i < numQuestions; ++i) {
int randIndex = rand() % numQuestions;
swap(indexes[i], indexes[randIndex]);
}
string ans;
for (int i = 0; i < numQuestions; ++i) {
int qIndex = indexes[i];
if(i==0){
cout << "\t\t\t Welcome to C++ Beginner QUIZZZZZ " << endl;
cin.ignore();
}
cout << questions[qIndex].question << endl;
getline(cin,ans);
cout << "You answered: '" << ans << "'" << endl;
if (questions[qIndex].question == "Name any one type of Programming Language.") {
if (ans != "High level" && ans != "Low level") {
cout << "Expected 'High level' or 'Low level'" << endl;
retake();
return;
}
} else {
if (ans != questions[qIndex].answer) {
cout << "Expected: '" << questions[qIndex].answer << "'" << endl;
retake();
return;
}
}
}
system("cls");
leaderboard.addScore(currentUser, 5);
leaderboard.saveToFile();
cout << "\n\n \t\t\t!!!!!Congratulations!!!!! \n\n";
intermediate();
}
void intermediate() {
quizLevel='I';
string ans;
cout << "\t\t\t Welcome to C++ Intermediate QUIZZZZZ " << endl;
cout << "Question NO 1:" << endl;
cout << "How to declare a variable in C++...........?" << endl;
cin >> ans;
if (ans == "datatype_variablename_=_value;") {
system("cls");
cout << "Question NO 2" << endl;
cout << "In How many ways can we initialize Variable in C++......" << endl;
cin >> ans;
if (ans == "Two") {
cout << "Question No 3" << endl;
cout << "Increments Type" << endl;
cin >> ans;
if (ans == "Two") {
cout << "\n\n \t\t!!!!!Congratulations!!!!!\n\n";
leaderboard.addScore(currentUser, 5);
leaderboard.saveToFile();
advance();
} else {
retake();
}
} else {
retake();
}
} else {
retake();
}
}
void advance() {
quizLevel='A';
string userProgramPath;
cout << "\t\t\t Welcome to C++ Advanced QUIZZZ" << endl;
cout << "Enter the path to the user's program file: ";
cin.ignore();
getline(cin, userProgramPath);
ifstream file(userProgramPath);
if (!file.is_open()) {
cerr << "Error: Could not open the file!" << endl;
}
string code(( istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
file.close();
vector< string> errors;
checkVariableNames(code, errors);
checkSemicolons(code, errors);
checkLibraryIncludes(code, errors);
if (errors.empty()) {
cout << "Your program is great!" << endl;
leaderboard.addScore(currentUser, 10);
leaderboard.saveToFile();
} else {
for (const auto& error : errors) {
cout << error << endl;
}
}
}
void retake() {
char ch;
cout << "\t\t\t ohhhhhhhh Wrong Answer !!!\n\n";
cout << "\t| Press R for Retake Quiz |\n";
cout << "\t| Press L for Learning Path |\n";
cin >> ch;
if (ch == 'R' || ch== 'r') {
cout << "\t| Which Level you want to retake"<<endl ;
cout << "\t| Press B for Beginner level |\n";
cout << "\t| Press I for Intermediate level |\n";
cout << "\t| Press A for Advanced level |\n";
cout << "\t\t\t Enter Your Choice : ";
cin >> quizLevel;
if (quizLevel == 'B') {
beginner();
} else if (quizLevel == 'I') {
intermediate();
} else {
advance();
}
} else {
cout << "https://www.w3schools.com/cpp/";
}
}
};
class QuizApp {
public:
void run() {
int choice;
while (true) {
showMenu();
cin >> choice;
switch (choice) {
case 1: {
if (user.loginUser()) {
quiz.startQuiz();
}
break;
}
case 2: {
user.registerUser();
quiz.startQuiz();
break;
}
case 3: {
system("cls");
cout << "\t\t\t THANK YOU! \n\n ";
return;
}
case 4: {
leaderboard.loadFromFile();
leaderboard.display();
break;
}
default: {
system("cls");
cout << "\t\t\t Please select a valid option: \n\n";
}
}
}
}
private:
User user;
Quiz quiz;
void showMenu() {
cout << "\t\t\t_____________________________________________________\n\n\n";
cout << "\t\t\t Welcome to the Login Page \n\n\n";
cout << "\t\t\t________________ MENU _________________________\n\n\n";
cout << " \n\n";
cout << "\t| Press 1 to LOGIN |\n";
cout << "\t| Press 2 to REGISTER |\n";
cout << "\t| Press 3 to EXIT |\n";
cout << "\t| Press 4 to VIEW LEADERBOARD |\n";
cout << "\n\t\t\t Please Enter your Choice : ";
}
};
void checkVariableNames(const string& code, vector<string>& errors) {
istringstream stream(code);
string word;
int lineNumber = 1;
regex variablePattern(R"((int|float|double|char|string)\s+([a-zA-Z_]\w*))");
while ( getline(stream, word)) {
smatch matches;
if ( regex_search(word, matches, variablePattern)) {
string variableName = matches[2];
if (! isalpha(variableName[0])) {
errors.push_back("Error on line " + to_string(lineNumber) + ": Variable '" + variableName + "' does not start with an alphabetic letter.");
}
}
lineNumber++;
}
};
void checkSemicolons(const string& code, vector< string>& errors) {
istringstream stream(code);
string word;
int lineNumber = 1;
while ( getline(stream, word)) {
string trimmed = word;
trimmed.erase(trimmed.find_last_not_of(" \n\r\t") + 1); // Trim trailing whitespace
if (!trimmed.empty() && trimmed.back() != ';' &&
(trimmed.find("int ") != string::npos ||
trimmed.find("float ") != string::npos ||
trimmed.find("double ") != string::npos ||
trimmed.find("char ") != string::npos ||
trimmed.find(" string ") != string::npos ||
trimmed.find("return") != string::npos)) {
errors.push_back("Error on line " + to_string(lineNumber) + ": Missing semicolon.");
}
lineNumber++;
}
};
void checkLibraryIncludes(const string& code, vector< string>& errors) {
set< string> requiredLibraries = {"<iostream>", "<vector>", "<string>"};
istringstream stream(code);
string line;
set< string> includedLibraries;
int lineNumber = 1;
while ( getline(stream, line)) {
regex includePattern(R"(^\s*#include\s*(<[^>]+>|"[^"]+")\s*)");
smatch matches;
if ( regex_search(line, matches, includePattern)) {
includedLibraries.insert(matches[1]);
}
lineNumber++;
}
for (const auto& lib : requiredLibraries) {
if (includedLibraries.find(lib) == includedLibraries.end()) {
errors.push_back("Error: Missing required library " + lib);
}
}
};
int main() {
QuizApp app;
app.run();
return 0;
}