-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
114 lines (90 loc) · 3.72 KB
/
Main.cpp
File metadata and controls
114 lines (90 loc) · 3.72 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
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
// Includes external libs.
#include <iostream>
#include <string>
#include <random>
#include <limits>
#include <locale>
#include <sstream>
#include <stdlib.h>
#include <unistd.h> // for usleep
// Udefines max since 1 lib got confused.
#undef max
// Prevents writing ::std in front of everything.
using namespace std;
// Lists/arrays and variables for the project.
string list[8] = { "No, according to fate.", "Yes, according to fate.", "It is certain.", "Without a doubt.", "Uncertainly...", " Undoubtedly!", " Probably...", " Perhaps..." };
string response = list[rand() % 8];
string notimp;
char yesno;
// Main integer
int main() {
// Launguage stuff. Didn't know much about this (copy-paste).
try {
locale::global(locale("C")); // Uses the locale "C".
} catch (const std::runtime_error& e) {
cerr << "Error: " << e.what() << endl;
return 1;
}
// A random seed. Uses a seed to make results random.
srand(static_cast<unsigned int>(time(0)));
// a do-while loop. makes the program "restartable".
do {
system("clear");
// Prints text.
cout << "\nMagic 8 Ball\n";
cout << "-------------\n";
// Only a measure to get unknown letters into "cout" to be included, but really, it can probably be done without.
string inkl = "Enter a question to be predicted:\n\n";
cout << inkl;
// I got some help from someone else with this. I don't really know what getline does, but I guess it includes the entire line (with spaces).
getline(cin, notimp);
// Prints the "-" according to how long the input is +13 because "you answered:" is included.
for (size_t i = 0; i <= notimp.length()+13; i++) {
cout << '-';
}
// cout includes "unknown" ASCII-symbols.
cout << '\n';
cout << "You answered: " << notimp << "\n\n";
cout << "The 8 ball is shaking!\n";
// Makes the program wait 5 deciseconds.
usleep(500000); // usleep uses microseconds as an argument
// Repeats "." three times at 5 decisecond intervals.
for (int dot = 0; dot < 3; dot++) {
cout << ".\n";
usleep(500000);
};
// Selects a random object in the array "list" using the seed mentioned earlier.
int index = rand() % (sizeof(list) / sizeof(list[0]));
// To make the 8-ball vibe:
string response = list[index];
string balltop = " ,.----.,\n .´' `.\n| .--. |\n";
string ballbott = "| '--' |\n `._ _.´ \n `'----'´ \n";
cout << balltop;
cout << response << "\n";
cout << ballbott;
// Asks the user for a new prediction.
string inkl2 = "\nDo you want to ask a new question (y for yes, n for no)?\n\n";
cout << inkl2;
cin >> yesno;
// Removes unnecessary input if it hasn't been removed automatically before.
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// If the variable is "y" or "Y", this is printed...
if (yesno == 'y' || yesno == 'Y') {
cout << "\nRestarting...\n";
cout << "-------------------\n\n";
system("clear");
}
// If the variable is "n" or "N", this is printed and the program is stopped.
if (yesno == 'n' || yesno == 'N') {
cout << "\nExiting...\n";
usleep(1000000); // usleep tar mikrosekunder som argument
break;
}
// End of do-while-loop.
} while (yesno == 'y' || yesno == 'Y');
yesno = ' ';
cin.clear();
// Returns zero
return 0;
}