-
Notifications
You must be signed in to change notification settings - Fork 0
/
EasySchoolTest.cpp
94 lines (89 loc) · 3.2 KB
/
EasySchoolTest.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
#include <cstring>
#include <ctype.h>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
#include <bits/stdc++.h>
std::ifstream fin("questions.txt");
std::ifstream afin("answers.txt");
//We input the questions with fin and answers with afin
using namespace std;
char student_answer[10],correct_answer[10];
int nr_of_lines=0,nr_of_answers=0,questions_array[10];
//We will lower both the correct and the student answer so we can compare them and not be case-sensitive
void LOWER() {
for(int i = 0; student_answer[i]; i++)
student_answer[i] = tolower(student_answer[i]);
for(int i = 0; correct_answer[i]; i++)
correct_answer[i] = tolower(correct_answer[i]);
}
int check_if_every_value_is_different() {
int original_val;
for (unsigned i = 0; i < 9; i++) {
original_val = questions_array[i];
for (unsigned k = i + 1; k < 9; k++)
if (questions_array[i] == questions_array[k]){
return 0;
} }
return 1;
}
int main() {
//If the student answers wrong all questions he will get an 1 (out of 10)
int grade=1;
string line;
int current_random;
/*We are checking how many questions are there, so we dont have
to store more than 1 question at a time*/
while (getline(fin,line)) //while there are more questions we increment the variable
{
nr_of_lines++;
}
while (getline(afin,line)) //while there are more questions we increment the variable
{
nr_of_answers++;
}
if (nr_of_lines<9 || nr_of_answers!=nr_of_lines) {
cout<<"The questions file has less than 9 questions or the number of questions and answers is different"<<endl;
cout<<"Please close the program and put at least 9 questions."<<endl;
cin>>line; }
else
{
srand(time(NULL)); //makes it more random
fin.close();
afin.close();
std::ifstream fin("questions.txt");
std::ifstream afin("answers.txt");
//we chose the 9 questions and store them in an array
for(int i=0; i<9; i++) {
current_random = rand() % nr_of_lines;// 0 to nr of lines
questions_array[i]=current_random;
// cout<<current_random<<endl;
}
//while a question appears at least 2 times we repick the questions
while(check_if_every_value_is_different()==0)
for(int i=0; i<9; i++) {
current_random = rand() % nr_of_lines;
questions_array[i]=current_random;
}
//Asking the student and verifying the the 9 answers
for(int i=0; i<9; i++) {
fin.close();
std::ifstream fin("questions.txt");
//storing just one question at a time
for (int j=0; j<questions_array[i]; j++)
getline(fin,line);
cout<<line<<endl;
for (int j=0; j<questions_array[i]; j++)
afin>>correct_answer;
// cout<<correct_answer;
cin>>student_answer;
//only if the answers are different we make them both lowercase
if (strcmp(student_answer,correct_answer))
LOWER();
if (strcmp(student_answer,correct_answer)==0)
grade++;
}
cout<<"Your grade is "<<grade;
cin>>line;
}
}