-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathtranslateTest.cpp
587 lines (457 loc) · 17 KB
/
translateTest.cpp
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//*****************************************************************************************************
// American to English Translation Test
//
// The program reads the translation and tester information from text files and randomly selects
// three people from the list to take the test. The results are recorded along with the date
// taken for each person, and is automatically written back to the text file.
//
// Other files required:
// 1. Testers.txt - number of testers and the tester information
// 2. Translation.txt - number of translations and the translation information
//
//*****************************************************************************************************
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
//*****************************************************************************************************
const int NAME_SIZE = 20;
struct Translation {
string american;
string english;
};
struct Date {
int month;
int day;
int year;
};
struct Person {
char name[NAME_SIZE];
double score;
Date testTaken; // nested structure
};
Translation *readTranslation(const string &fileName, int &num);
Person *readTesters(const string &fileName, int &num);
void testingOptions(const Translation translate[], int numT, Person people[], int numP);
void takeTest(const Translation translate[], int numT, Person &p);
void displayTesters(const Person people[], int numP);
void writeTesters(const string &fileName, const Person people[], int numP);
void displayTranslateAnswers(const Translation translate[], int numT);
//*****************************************************************************************************
int main() {
int numT = 0,
numP = 0;
string fileName;
char studyEntry;
Translation *translate = nullptr;
Person *people = nullptr;
cout << fixed << setprecision(1);
translate = readTranslation("Translation.txt", numT);
people = readTesters("Testers.txt", numP);
displayTesters(people, numP);
cout << "\n---------------------------------------------------\n"
<< "\tAmerican to English Translation Test\n"
<< "---------------------------------------------------\n"
<< " Three people from this list will be randomly\n"
<< " selected to take the test \n\n"
<< "Would you like to study before the test? (Y/N)" << endl;
cin >> studyEntry;
studyEntry = toupper(studyEntry);
if (studyEntry == 'Y') {
displayTranslateAnswers(translate, numT);
cout << "\nPress enter to continue..." << endl;
cin.ignore();
cin.get();
cout << "Good Luck!" << endl;
} else {
cout << "\nGood Luck!" << endl;
}
testingOptions(translate, numT, people, numP);
cout << "===================================================\n"
<< "\t\tUpdated Information\n"
<< "===================================================";
displayTesters(people, numP);
writeTesters("Testers.txt", people, numP);
delete[] translate;
translate = nullptr;
delete[] people;
people = nullptr;
return 0;
}
//*****************************************************************************************************
Translation *readTranslation(const string &fileName, int &num) {
Translation *t = nullptr;
ifstream f(fileName);
if (f.is_open()) { // check if file is open before reading
f >> num;
f.ignore();
t = new Translation[num];
for (int i = 0; i < num; ++i) {
getline(f, t[i].american, ',');
getline(f, t[i].english);
}
f.close();
} else {
cerr << "Error: Unable to open file\n";
}
return t;
}
//*****************************************************************************************************
Person *readTesters(const string &fileName, int &num) {
Person *p = nullptr;
ifstream f(fileName);
if (f.is_open()) {
f >> num;
f.ignore();
p = new Person[num];
for (int i = 0; i < num; ++i) {
f.getline(p[i].name, NAME_SIZE);
f >> p[i].score;
f.ignore();
f >> p[i].testTaken.month;
f.ignore();
f >> p[i].testTaken.day;
f.ignore();
f >> p[i].testTaken.year;
f.ignore();
}
f.close();
} else {
cerr << "Error: Unable to open file\n";
}
return p;
}
//*****************************************************************************************************
void testingOptions(const Translation translate[], int numT, Person people[], int numP) {
const int NUM_TESTS = 3;
int randomPerson = 0,
month,
day,
year;
short seed;
seed = time(0);
srand(seed);
cout << "\n---------------------------------\n"
<< " Enter Today's Date\n"
<< "---------------------------------" << endl;
cout << "Enter month: ";
cin >> month;
cout << "Enter day: ";
cin >> day;
cout << "Enter year: ";
cin >> year;
cout << "\n\n---------------------------------\n"
<< " Questions\n"
<< "---------------------------------\n"
<< " Five American words will be\n"
<< " randomly selected from the list\n"
<< " Enter the English translation." << endl;
for (int i = 0; i < NUM_TESTS; ++i) {
randomPerson = rand() % numP;
people[randomPerson].testTaken.month = month;
people[randomPerson].testTaken.day = day;
people[randomPerson].testTaken.year = year;
cout << "\n=================================" << endl;
cout << setfill(' ')
<< setw(3) << left << "#" << setw(20) << "NAME" << "TEST TAKEN\n"
<< "---------------------------------" << endl;
cout << setw(3) << randomPerson + 1
<< setw(20) << people[randomPerson].name
<< people[randomPerson].testTaken.month << "/"
<< people[randomPerson].testTaken.day << "/"
<< people[randomPerson].testTaken.year << endl;
takeTest(translate, numT, people[randomPerson]);
}
}
//*****************************************************************************************************
void takeTest(const Translation translate[], int numT, Person &p) {
const int NUM_QUESTIONS = 5;
int randomQuestion = 0,
correct = 0;
double avg = 0;
string answer,
guess;
short seed;
seed = time(0);
srand(seed);
cout << "\n---------------------------------" << endl;
cout << setfill(' ')
<< setw(3) << left << "#" << setw(18) << "American" << "English\n"
<< "---------------------------------" << endl;
for (int i = 0; i < NUM_QUESTIONS; ++i) {
randomQuestion = rand() % numT;
cout << setfill(' ') << setw(3) << randomQuestion + 1
<< setfill('.') << setw(18) << translate[randomQuestion].american;
cin >> guess;
answer = translate[randomQuestion].english;
if (guess == answer) {
cout << "\n\t Correct!" << endl;
correct++;
} else {
cout << "\n\t Incorrect!\n\n"
<< "Answer: " << translate[randomQuestion].english << endl;
}
cout << endl;
}
avg = (static_cast<double>(correct) / NUM_QUESTIONS) * 100;
p.score = avg;
}
//*****************************************************************************************************
void displayTesters(const Person people[], int numP) {
cout << "\n---------------------------------------------------" << endl;
cout << setfill(' ')
<< setw(3) << left << "#" << setw(20) << "NAME" << setw(15) << "SCORE %" << "TEST TAKEN\n"
<< "---------------------------------------------------" << endl;
for (int i = 0; i < numP; ++i)
cout << setw(3) << i + 1
<< setw(20) << people[i].name
<< setw(15) << people[i].score
<< people[i].testTaken.month << "/"
<< people[i].testTaken.day << "/"
<< people[i].testTaken.year << endl;
}
//*****************************************************************************************************
void writeTesters(const string &fileName, const Person people[], int numP) {
ofstream f(fileName);
f << numP << endl;
for (int i = 0; i < numP; ++i)
f << people[i].name << "\n"
<< people[i].score << ","
<< people[i].testTaken.month
<< "/" << people[i].testTaken.day
<< "/" << people[i].testTaken.year << endl;
f.close();
}
//*****************************************************************************************************
void displayTranslateAnswers(const Translation translate[], int numT) {
cout << "\n---------------------------------\n"
<< setw(3) << left << "#" << setw(18) << "American" << "English\n"
<< "---------------------------------" << endl;
for (int i = 0; i < numT; ++i)
cout << setfill(' ') << setw(3) << i + 1
<< setfill('.') << setw(18) << translate[i].american
<< translate[i].english << endl;
}
//*****************************************************************************************************
/*
---------------------------------------------------
# NAME SCORE % TEST TAKEN
---------------------------------------------------
1 Steve Smith 56.6 11/11/2019
2 Sue Jones 10.0 11/11/2011
3 Li Ying 0.0 11/11/2011
4 Kun Joom 20.0 11/11/2021
5 Joe Bush 10.0 11/11/2021
6 Kim Long 0.0 11/11/2011
7 Fred Ring 33.3 5/5/2019
8 Frank Pearse 20.0 11/11/2011
9 Helen Hu 0.1 11/11/2011
10 Mark James 0.0 11/11/2021
---------------------------------------------------
American to English Translation Test
---------------------------------------------------
Three people from this list will be randomly
selected to take the test.
Would you like to study before the test? (Y/N)
N
Good Luck!
*****************************************************************************************************
---------------------------------------------------
# NAME SCORE % TEST TAKEN
---------------------------------------------------
1 Steve Smith 56.6 11/11/2019
2 Sue Jones 10.0 11/11/2011
3 Li Ying 0.0 11/11/2011
4 Kun Joom 20.0 11/11/2021
5 Joe Bush 10.0 11/11/2021
6 Kim Long 0.0 11/11/2011
7 Fred Ring 33.3 5/5/2019
8 Frank Pearse 20.0 11/11/2011
9 Helen Hu 0.1 11/11/2011
10 Mark James 0.0 11/11/2021
---------------------------------------------------
American to English Translation Test
---------------------------------------------------
Three people from this list will be randomly
selected to take the test.
Would you like to study before the test? (Y/N)
Y
---------------------------------
# American English
---------------------------------
1 chips.............crisps
2 french fries......chips
3 sidewalk..........pavement
4 cookie............biscuit
5 apartment.........flat
6 elevator..........lift
7 gas...............petrol
8 soccer............football
9 movie.............film
10 sweater...........jumper
11 trunk.............boot
12 hood..............bonnet
13 last name.........surname
14 restroom..........toilet
15 cart..............trolley
16 candy.............sweet
17 vacation..........holiday
18 subway............underground
19 pants.............trousers
20 mailbox...........postbox
Press enter to continue...
*****************************************************************************************************
---------------------------------------------------
# NAME SCORE % TEST TAKEN
---------------------------------------------------
1 Steve Smith 56.6 11/11/2019
2 Sue Jones 10.0 11/11/2011
3 Li Ying 0.0 11/11/2011
4 Kun Joom 20.0 11/11/2021
5 Joe Bush 10.0 11/11/2021
6 Kim Long 0.0 11/11/2011
7 Fred Ring 33.3 5/5/2019
8 Frank Pearse 20.0 11/11/2011
9 Helen Hu 0.1 11/11/2011
10 Mark James 0.0 11/11/2021
---------------------------------------------------
American to English Translation Test
---------------------------------------------------
Three people from this list will be randomly
selected to take the test.
Would you like to study before the test? (Y/N)
Y
---------------------------------
# American English
---------------------------------
1 chips.............crisps
2 french fries......chips
3 sidewalk..........pavement
4 cookie............biscuit
5 apartment.........flat
6 elevator..........lift
7 gas...............petrol
8 soccer............football
9 movie.............film
10 sweater...........jumper
11 trunk.............boot
12 hood..............bonnet
13 last name.........surname
14 restroom..........toilet
15 cart..............trolley
16 candy.............sweet
17 vacation..........holiday
18 subway............underground
19 pants.............trousers
20 mailbox...........postbox
Press enter to continue...
Good Luck!
---------------------------------
Enter Today's Date
---------------------------------
Enter month: 4
Enter day: 14
Enter year: 2022
---------------------------------
Questions
---------------------------------
Five American words will be
randomly selected from the list.
Enter the English translation.
=================================
# NAME TEST TAKEN
---------------------------------
4 Kun Joom 4/14/2022
---------------------------------
# American English
---------------------------------
15 cart..............trolley numbers added to easily locate answers from the list
Correct!
5 apartment.........flat
Correct!
4 cookie............biscuit
Correct!
17 vacation..........holiday
Correct!
16 candy.............sweet
Correct!
=================================
# NAME TEST TAKEN
---------------------------------
5 Joe Bush 4/14/2022
---------------------------------
# American English
---------------------------------
6 elevator..........lift
Correct!
17 vacation..........holiday
Correct!
5 apartment.........complex
Incorrect!
Answer: flat incorrect answers are displayed with the correct answer
16 candy.............yumyum
Incorrect!
Answer: sweet
8 soccer............football
Correct!
=================================
# NAME TEST TAKEN
---------------------------------
9 Helen Hu 4/14/2022
---------------------------------
# American English
---------------------------------
20 mailbox...........postbox
Correct!
3 sidewalk..........pavement
Correct!
19 pants.............trousers
Correct!
20 mailbox...........postbox
Correct!
16 candy.............sweeties
Incorrect!
Answer: sweet
===================================================
Updated Information
===================================================
---------------------------------------------------
# NAME SCORE % TEST TAKEN
---------------------------------------------------
1 Steve Smith 56.6 11/11/2019
2 Sue Jones 10.0 11/11/2011
3 Li Ying 0.0 11/11/2011
4 Kun Joom 100.0 4/14/2022
5 Joe Bush 60.0 4/14/2022
6 Kim Long 0.0 11/11/2011
7 Fred Ring 33.3 5/5/2019
8 Frank Pearse 20.0 11/11/2011
9 Helen Hu 80.0 4/14/2022
10 Mark James 0.0 11/11/2021
*****************************************************************************************************
10 updated file with new scores and test dates
Steve Smith
56.6,11/11/2019
Sue Jones
10,11/11/2011
Li Ying
0,11/11/2011
Kun Joom
100,4/14/2022
Joe Bush
60,4/14/2022
Kim Long
0,11/11/2011
Fred Ring
33.3,5/5/2019
Frank Pearse
20,11/11/2011
Helen Hu
80,4/14/2022
Mark James
0,11/11/2021
*/