Repository navigation
Expand file tree
/
Copy pathedit-question.cpp
More file actions
175 lines (151 loc) · 4.45 KB
/
Copy pathedit-question.cpp
File metadata and controls
175 lines (151 loc) · 4.45 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
#include <iostream>
#include <string>
#include <fstream>
#include "transform-validation.h"
using namespace std;
#include "edit-question.h"
string searchFileName(string& fileName, string level) {
if (level == "1" || level == "2" || level == "3") {
fileName = "question1.txt";
return fileName;
}
if (level == "4" || level == "5" || level == "6") {
fileName = "question2.txt";
return fileName;
}
if (level == "7" || level == "8" || level == "9" || level == "10") {
fileName = "question3.txt";
return fileName;
}
return fileName;
}
void editQuestion(string level, string& fileName, string& id, string& question, string& A, string& B, string& C, string& D,
string& correctAns, string& difficultly, string& category, bool& isFound) {
ifstream sourceFile;
ofstream destinationFile;
string searchId;
string newData;
cout << "What is id number of question? ";
cin >> searchId;
cin.ignore();
isEmpty(searchId);
size_t size = searchId.size();
for (size_t i = 2; i <size; i++) {
searchId[i] = tolower(searchId[i]);
}
cout << "What is difficultly of question? ";
getline(cin, level);
//Validation
isEmpty(level);
while (level != "1" && level != "2" && level != "3" && level != "4" && level != "5" && level != "6" && level != "7" && level != "8" && level != "9" && level != "10") {
cout << "Invalid input! Try again: ";
getline(cin, level);
}
fileName = searchFileName(fileName, level);
sourceFile.open(fileName);
if (!sourceFile.is_open()) {
cout << "Source file doesn't open." << endl;
return;
}
destinationFile.open("new.txt");
if (!destinationFile.is_open()) {
cout << "Destination file doesn't open." << endl;
return;
}
//flag for empty file
bool flag = true;
while (getline(sourceFile, id) && getline(sourceFile, question) && getline(sourceFile, A) && getline(sourceFile, B) && getline(sourceFile, C)
&& getline(sourceFile, D) && getline(sourceFile, correctAns) && getline(sourceFile, difficultly) && getline(sourceFile, category)) {
if (searchId == id) {
isFound = true;
//Print question which has to edit
cout << question << endl;
cout << "A) " << A << endl;
cout << "B) " << B << endl;
cout << "C) " << C << endl;
cout << "D) " << D << endl;
cout << "The correct answer is: " << correctAns << endl;
cout << "The category is: " << category << endl;
cout << "\n";
if (flag) {
flag = false;
destinationFile << id << '\n';
}
else {
destinationFile << '\n' << id << '\n';
}
cout << "Enter a question: ";
getline(cin, newData);
isEmpty(newData);
destinationFile << newData << '\n';
cout << "Enter first answer: ";
getline(cin, newData);
isEmpty(newData);
destinationFile << newData << '\n';
cout << "Enter second answer: ";
getline(cin, newData);
isEmpty(newData);
destinationFile << newData << '\n';
cout << "Enter third answer: ";
getline(cin, newData);
isEmpty(newData);
destinationFile << newData << '\n';
cout << "Enter fourth answer: ";
getline(cin, newData);
isEmpty(newData);
destinationFile << newData << '\n';
cout << "Which answer is correct? ";
getline(cin, newData);
isEmpty(newData);
newData[0] = tolower(newData[0]);
while (newData != "a" && newData != "b" && newData != "c" && newData != "d") {
cout << "Invalid input! Try again: ";
getline(cin, newData);
newData[0] = tolower(newData[0]);
}
destinationFile << newData << '\n';
destinationFile << level << '\n';
cout << "What is the category of the question? ";
getline(cin, newData);
isEmpty(newData);
transformCat(newData,1);
destinationFile << newData;
}
else {
if (flag) {
flag = false;
destinationFile << id << '\n';
}
else {
destinationFile << '\n' << id << '\n';
}
destinationFile << question << '\n';
destinationFile << A << '\n';
destinationFile << B << '\n';
destinationFile << C << '\n';
destinationFile << D << '\n';
destinationFile << correctAns << '\n';
destinationFile << difficultly << '\n';
destinationFile << category;
}
}
sourceFile.close();
destinationFile.close();
string newName;
if (fileName == "question1.txt") {
remove("question1.txt");
newName=rename("new.txt", "question1.txt");
return;
}
if (fileName == "question2.txt") {
remove("question2.txt");
newName=rename("new.txt", "question2.txt");
return;
}
if (fileName == "question3.txt") {
remove("question3.txt");
newName=rename("new.txt", "question3.txt");
return;
}
return;
}