-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
223 lines (216 loc) · 7.21 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
// TODO(dflore36): Personality Quiz
#include <set>
#include <vector>
#include <map>
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
#include "driver.h"
#include <cstdlib>
using namespace std;
int concat(int a, int b) {
string s1 = to_string(a);
string s2 = to_string(b);
string s = s1 + s2;
int c = stoi(s);
return c;
}
void saveFactors(string& singlePair, map<char, int>& factorMap) {
char letter;
vector<int> scoreVec;
int sign = 0;
int newScore;
for (char& c : singlePair) {
if ((c >='a' && c <='z') || (c >='A' && c <='Z')) {
letter = c;
}
if (c == '-') { // if value is negative
sign = -1;
} // If digit
if (isdigit(c)) {
stringstream stream;
stream << c;
int b;
stream >> b;
scoreVec.push_back(b);
}
}
if (scoreVec.size() > 1) {
int begin = scoreVec.at(0);
for (unsigned j = 1 ; j < scoreVec.size(); j++) {
begin = concat(begin, scoreVec[j]);
}
newScore = begin;
}
else newScore = scoreVec[0];
if (sign == -1) {
newScore = newScore * -1;
}
factorMap.emplace(letter, newScore);
}
map<char, int> getFactors(string factors) {
map<char, int> factorMap;
stringstream stream(factors);
string singlePair;
while (stream >> singlePair) { // Pass one factor pair at a time
saveFactors(singlePair, factorMap); // Save the pair in the map
}
return factorMap;
}
void readFile(string fileName, set<Question>& theQuestions,
map<char, int>& factorScoreMap, set<Person>& thePeople, int fileType) {
ifstream inFS;
inFS.open(fileName);
if (!inFS.is_open()) {
throw runtime_error("File cant be accessed");
}
string line;
getline(inFS, line);
while (!inFS.eof()) {
stringstream parser(line);
string questionName;
string factorsScore;
getline(parser, questionName, '.'); // obtains qustion or Name
getline(parser, factorsScore); // Obatains factors or scores
if (fileType == 1) {
Question newQuestionStruct;
newQuestionStruct.questionText = questionName;
factorScoreMap = getFactors(factorsScore);
newQuestionStruct.factors = factorScoreMap;
theQuestions.emplace(newQuestionStruct);
}
if (fileType == 2) {
Person newPersonStruct;
newPersonStruct.name = questionName;
factorScoreMap = getFactors(factorsScore);
newPersonStruct.scores = factorScoreMap;
thePeople.emplace(newPersonStruct);
}
getline(inFS, line);
}
}
int main() {
set<Question> theQuestions;
set<Person> blank;
map<char, int> usersFactorScores;
Person theChosenOne;
map<char, int> FactorsScoresMap;
map<Question, int> indivualsScores;
bool over = false;
int numberQuestions;
int userAnswer;
int userTestNumber;
Question questionMember;
set<Person> thePeople;
Person theBEST;
readFile("questions.txt", theQuestions, FactorsScoresMap, blank, 1);
cout << "Welcome to the Personality Quiz!" << endl << endl;
cout << "Choose number of questions: ";
cin >> numberQuestions;
for (int i = 0; i < numberQuestions; i++) {
cout << endl << "How much do you agree with this statement?" << endl;
questionMember = randomQuestionFrom(theQuestions);
cout << "\"" << questionMember.questionText << ".\"" << endl << endl;
cout << "1. Strongly disagree" << endl;
cout << "2. Disagree" << endl;
cout << "3. Neutral" << endl;
cout << "4. Agree" << endl;
cout << "5. Strongly agree" << endl << endl;
cout << "Enter your answer here (1-5): ";
cin >> userAnswer;
indivualsScores.emplace(questionMember, userAnswer);
}
usersFactorScores = scoresFrom(indivualsScores);
while (!over) {
cout << "1. BabyAnimals" << endl;
cout << "2. Brooklyn99" << endl;
cout << "3. Disney" << endl;
cout << "4. Hogwarts" << endl;
cout << "5. MyersBriggs" << endl;
cout << "6. SesameStreet" << endl;
cout << "7. StarWars" << endl;
cout << "8. Vegetables" << endl;
cout << "9. mine" << endl;
cout << "0. To end program." << endl << endl;
cout << "Choose test number (1-9, or 0 to end): ";
cin >> userTestNumber;
if (userTestNumber == 1) {
readFile("BabyAnimals.people", theQuestions,
FactorsScoresMap, thePeople, 2);
theChosenOne = mostSimilarTo(usersFactorScores, thePeople);
Person theBEST = theChosenOne;
cout << "You got " << theBEST.name << "!" << endl << endl;
thePeople.clear();
}
if (userTestNumber == 2) {
readFile("Brooklyn99.people", theQuestions,
FactorsScoresMap, thePeople, 2);
theChosenOne = mostSimilarTo(usersFactorScores, thePeople);
cout << "You got " << theChosenOne.name << "!" << endl << endl;
thePeople.clear();
}
if (userTestNumber == 3) {
readFile("Disney.people", theQuestions,
FactorsScoresMap, thePeople, 2);
theChosenOne = mostSimilarTo(usersFactorScores, thePeople);
Person theBEST = theChosenOne;
cout << "You got " << theBEST.name << "!" << endl << endl;
thePeople.clear();
}
if (userTestNumber == 4) {
readFile("Hogwarts.people", theQuestions,
FactorsScoresMap, thePeople, 2);
theChosenOne = mostSimilarTo(usersFactorScores, thePeople);
Person theBEST = theChosenOne;
cout << "You got " << theBEST.name << "!" << endl << endl;
thePeople.clear();
}
if (userTestNumber == 5) {
readFile("MyersBriggs.people", theQuestions,
FactorsScoresMap, thePeople, 2);
theChosenOne = mostSimilarTo(usersFactorScores, thePeople);
Person theBEST = theChosenOne;
cout << "You got " << theBEST.name << "!" << endl << endl;
thePeople.clear();
}
if (userTestNumber == 6) {
readFile("SesameStreet.people", theQuestions,
FactorsScoresMap, thePeople, 2);
theChosenOne = mostSimilarTo(usersFactorScores, thePeople);
Person theBEST = theChosenOne;
cout << "You got " << theBEST.name << "!" << endl << endl;
thePeople.clear();
}
if (userTestNumber == 7) {
readFile("StarWars.people", theQuestions,
FactorsScoresMap, thePeople, 2);
theChosenOne = mostSimilarTo(usersFactorScores, thePeople);
Person theBEST = theChosenOne;
cout << "You got " << theBEST.name << "!" << endl << endl;
thePeople.clear();
}
if (userTestNumber == 8) {
readFile("Vegetables.people", theQuestions,
FactorsScoresMap, thePeople, 2);
theChosenOne = mostSimilarTo(usersFactorScores, thePeople);
Person theBEST = theChosenOne;
cout << "You got " << theBEST.name << "!" << endl << endl;
thePeople.clear();
}
if (userTestNumber == 9) {
readFile("mine.people", theQuestions,
FactorsScoresMap, thePeople, 2);
theChosenOne = mostSimilarTo(usersFactorScores, thePeople);
Person theBEST = theChosenOne;
cout << "You got " << theBEST.name << "!" << endl << endl;
thePeople.clear();
}
if (userTestNumber == 0) {
cout << "Goodbye!" << endl;
over = true;
}
}
return 0;
cout << endl;
}