-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
279 lines (203 loc) · 6.47 KB
/
main.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
#include <iostream>
#include <string>
#include <fstream>
#include "Stats.h"
#include "Gen.h"
using namespace std;
void credits();
Gen nameValidatenLoad();
void inputValidation(string &name);
void alterStats(Stats *, int choice);
void menu();
string selection(Gen &);
void validateChoice(string& choice);
void gennUpdate(string choice, int&, Gen&); //generate question/display stats n change stats depending on answer
int main()
{
string choice;
int i = 0;
char cont = 'y';
credits(); //first screen where user sees credit and must answer Y/y in order to continue, any other answer will terminate program
Gen userStat = nameValidatenLoad();
cout << userStat; //fine here
for (i = 0; i < 1; i++)
{
menu();
choice = selection(userStat); //validate and return choice
gennUpdate(choice, i, userStat); //excludes n/N which exits program
//menu();
//choice = selection(userStat);
}
system("pause");
return 0;
}
void credits()
{
string cont;
cout << "***********************" << endl;
cout << "***********************" << endl;
cout << "***********************" << endl;
cout << "****** ******" << endl;
cout << "******TheMathGame******" << endl;
cout << "****** by ******" << endl;
cout << "****** JingyiTay ******" << endl;
cout << "****** ******" << endl;
cout << "***********************" << endl;
cout << "***********************" << endl;
cout << "***********************" << endl;
cout << "y/Y to continue, other keys to exit\n";
getline(cin, cont);
if (cont != "y" && cont != "Y")
{
exit(0);
}
}
void alterStats(Stats *userStat, int choice) //use of pointers to access Stats object & choice to indicate user's action
{
}
Gen nameValidatenLoad()
{
string name;
cout << "Enter your name and press <ENTER>\n";
getline(cin, name);
inputValidation(name);
Gen userStat(name); //create Stats object(userStat)
return userStat;
}
string selection(Gen &userStat)
{
string choice; //user choice input
getline(cin, choice);
validateChoice(choice);
return choice;
}
void validateChoice(string& choice) //function header(same as function prototype but has body to define function); function inside selection
{
//cout << switchChoice << endl;
int counter = 0;
char choiceChar;
while (counter == 0) //has to be less than length because max index is length - 1
{ // or cond can be (counter <= userInputLength - 1)
while (choice.length() > 1 || choice.length() == 0) //if choice input more than 1 char or 0 capture whitespace?
{
cout << "enter the appropriate selection \n";
getline(cin, choice);
}
choiceChar = choice[0]; //since only 1 char, take the 1 char stored at index 0 in string
while (counter < 1)
{
if (choiceChar == 49 || choiceChar == 50 || choiceChar == 51 || choiceChar == 52 || choiceChar == 53)
{
counter++;
}
else if (choiceChar == 'n' || choiceChar == 'N')
{
exit(0);
}
else
{
cout << "that is an invalid choice \n";
cout << "enter the appropriate selection \n";
getline(cin, choice);
while (choice.length() > 1 || choice.length() == 0) //if choice input more than 1 char or 0 capture whitespace?
{
cout << "enter the appropriate selection \n";
getline(cin, choice);
}
choiceChar = choice[0]; //since only 1 char, take the 1 char stored at index 0 in string
}
}
}
}
void inputValidation(string &name) //function header(same as function prototype but has body to define function)
{
int userInputLength;
int counter = 0;
userInputLength = name.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*/
system("cls");
cout << " you did not enter anything" << endl;
cout << "please enter it again" << endl;
getline(cin, name); //get another entry if wrong
userInputLength = name.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 (!(isalpha(name[counter]))) //note ! in 1st outer parenthesis
{
cout << "you did not enter a proper name" << endl;
cout << "please enter it again" << endl;
getline(cin, name);
userInputLength = name.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, name); //get another entry if wrong
userInputLength = name.length(); //check again and compares with condition, if cond still true, loop again
}
}
else
{
counter = counter + 1; //or counter++;
}
}
system("cls");
}
void menu()
{
cout << "*****CHOOSE A PROBLEM*****" << endl;
cout << "**************************" << endl;
cout << "**************************" << endl;
cout << "***** *****" << endl;
cout << "***** 1. Add *****" << endl;
cout << "***** 2. Subtract *****" << endl;
cout << "***** 3. Divide *****" << endl;
cout << "***** 4. Multiply *****" << endl;
cout << "***** 5. Stats *****" << endl;
cout << "***** n/N to quit *****" << endl;
cout << "***** *****" << endl;
cout << "**************************" << endl;
cout << "**************************" << endl;
}
void gennUpdate(string choice, int &i, Gen &genQns)
{
char choiceChar = choice[0];
char rightOrWrong; //accepts 1 or 0 to indicate correct or wrong
ofstream outData;
if (choiceChar == 49)
{
rightOrWrong = genQns.Add();
//genQns.update(rightOrWrong); //update stats based on rightOrWrong(1 or 0)
genQns.saveStats(outData);
--i;
}
else if (choiceChar == 50)
{
rightOrWrong = genQns.Subtract();
//genQns.update(rightOrWrong);
genQns.saveStats(outData);
--i;
}
else if (choiceChar == 51)
{
rightOrWrong = genQns.Mult();
//genQns.update(rightOrWrong);
genQns.saveStats(outData);
--i;
}
else if (choiceChar == 52)
{
rightOrWrong = genQns.Divide(); //generate division question; check answer and output 1 or 0;
//genQns.update(rightOrWrong);
genQns.saveStats(outData);
--i;
}
else if (choiceChar == 53)
{
cout << genQns;
--i;
}
}