-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathriddles.cc
More file actions
46 lines (41 loc) · 1.16 KB
/
riddles.cc
File metadata and controls
46 lines (41 loc) · 1.16 KB
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
#include "riddles.h"
using namespace std;
Riddles::Riddles() {
cout << endl;
riddle("What gives you the strength and power to walk through walls?", "door");
cout << endl;
riddle("What food goes on and on without a beginning, middle or end?", "donut");
riddle("What has a head and a tail but no legs?", "coin");
riddle("What is the answer to life, the universe, and everything?", "42");
}
void Riddles::riddle(const string &question, const string &answer){
string s;
string line;
int guesses = 0;
bool solved = 0;
cout << endl << question << endl;
while (getline(cin,line)){
fixString(s);
++ guesses;
stringstream strstrm {line};
while (strstrm >> s){
if (s == answer + "s" || s == answer) {
solved = true;
break;
}
}
if (solved) break;
}
while (guesses >= -1){
cout << "\u001b[1A\u001b[2K ";
--guesses;
}
}
void Riddles::fixString(string &s){
int len = s.length();
for (int i = 0; i < len; ++ i){
if (s[i] >= 'A' && s[i] <= 'Z'){
s[i] = s[i] + 'a' - 'A';
}
}
}