-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gen.cpp
167 lines (131 loc) · 4.12 KB
/
Gen.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
#include "Gen.h"
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cctype>
using namespace std;
Gen::Gen()
{
}
Gen::Gen(string userName)
{
ifstream inData;
ofstream outData;
loadUser(userName, inData,outData);
}
char Gen::Add()
{
srand((unsigned int)time(NULL));
num1 = rand() % 10;
num2 = rand() % 10;
theCorrectAnswer = num1 + num2; //com generate answer
cout << num1 << " + " << num2 << " = ";
getline(cin, theUserResponse);
//validate the userAnswer
validateUserInput(theUserResponse);
rightorWrong = checkTheAnswer(theUserResponse, &theCorrectAnswer, &rightorWrong); //decided whether 1 or 0; rightorWrong in void Genupdate in main.cpp
updateStats(rightorWrong); //update stats based on 1 or 0, in stats.h
saveStats(outfile); //in stats.h
return rightorWrong;
}
char Gen::Mult()
{
srand((unsigned int)time(NULL));
num1 = rand() % 10;
num2 = rand() % 10;
theCorrectAnswer = num1 * num2;
cout << num1 << " * " << num2 << " = ";
getline(cin, theUserResponse);
//validate the userAnswer
validateUserInput(theUserResponse);
rightorWrong = checkTheAnswer(theUserResponse, &theCorrectAnswer, &rightorWrong);
updateStats(rightorWrong);
saveStats(outfile);
return rightorWrong;
}
char Gen::Subtract()
{
num1 = 0;
num2 = 0;
theCorrectAnswer = 0;
do
{
srand((unsigned int)time(NULL));
num1 = rand() % 10 + 1;
num2 = rand() % 10;
theCorrectAnswer = num1 - num2;
} while (num1 < num2);
cout << num1 << " - " << num2 << " = ";
getline(cin, theUserResponse);
//validate the userAnswer
validateUserInput(theUserResponse);
rightorWrong = checkTheAnswer(theUserResponse, &theCorrectAnswer, &rightorWrong); //rightorWrong in void Genupdate in main.cpp
updateStats(rightorWrong);
saveStats(outfile);
return rightorWrong;
}
char Gen::Divide()
{
srand((unsigned int)time(NULL));
num1 = rand() % 10;
num2 = rand() % 10 + 1;
theCorrectAnswer = num1;
cout << num1*num2 << " / " << num2 << " = ";
getline(cin, theUserResponse);
//validate the userAnswer
validateUserInput(theUserResponse);
checkTheAnswer(theUserResponse, &theCorrectAnswer,&rightorWrong);
updateStats(rightorWrong);
saveStats(outfile);
return rightorWrong;
}
void Gen::validateUserInput(string &theUserResponse)
{
int userInputLength;
int counter = 0;
userInputLength = theUserResponse.length(); //how many characters in string var userInput
while (userInputLength == 0) //check if they typed nothing, just press enter, first validation
{ /* use while than If since it loops until user gives right answer, user stuck in loop*/
cout << " you did not enter anything" << endl;
cout << "please enter it again" << endl;
getline(cin, theUserResponse); //get another entry if wrong
userInputLength = theUserResponse.length(); //check again and compares with condition, if cond still true, loop again
}
while (counter < userInputLength) /*has to be less than length because max index is length - 1*/
{ // or cond can be (counter <= userInputLength - 1)
if (!(isdigit(theUserResponse[counter]))) //note ! in 1st outer parenthesis
{
cout << "you did not enter a proper digit" << endl;
cout << "please enter it again" << endl;
getline(cin, theUserResponse);
userInputLength = theUserResponse.length();
counter = 0;
while (userInputLength == 0) //check if they typed nothing, just press enter, first validation
{ /* use while than If since it loops until user gives right answer, user stuck in loop*/
cout << " you did not enter anything" << endl;
cout << "please enter it again" << endl;
getline(cin, theUserResponse); //get another entry if wrong
userInputLength = theUserResponse.length(); //check again and compares with condition, if cond still true, loop again
}//end of nested loop
}
else
{
counter = counter + 1; //or counter++;
}
}
}
char Gen::checkTheAnswer(string theUserResponse, int *theCorrectAnswer, char *rightorWrong) //check answer and issue 1 or 0
{
int userAnswer = stoi(theUserResponse);
if (userAnswer == *theCorrectAnswer)
{
*rightorWrong = '1';
}
else
{
*rightorWrong = '0';
}
return *rightorWrong;
}