-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
139 lines (110 loc) · 4.13 KB
/
main.cpp
File metadata and controls
139 lines (110 loc) · 4.13 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
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
#include <iostream>
using namespace std;
/*
* This is a simple multiple choice quiz with 5 questions.
* The user will interact with it via the command line, and
* receive a score at the end.
*
* They should enter A,B,C or D (upper case only for now) to answer.
*
* 1) First we need to define a list of questions, and their corresponding answers.
* 2) We then need to present the questions to the user one after the other, and have them respond.
* 3) We should calculate their score at the end and show it to them.
*
* Edge cases: User types something other than A,B,C or D - we should ask them the same question again in this case.
*
*
*/
// Define the questions
// Fixed size array
const string questions[] = {
"Question 1: \nWhat is the first step a compiler takes when processing code?\n\n"
"A: Running the code immediately.\n"
"B: Breaking the code into smaller pieces (tokens)\n"
"C: Displaying an error message\n"
"D: Saving the code as a text file\n",
"Question 2: \nWhat does a compiler produce when it finishes translating your code?\n\n"
"A: A text document.\n"
"B: Machine code or executable file.\n"
"C: An error message.\n"
"D: A printed book.\n",
"Question 3: \nWhat is the difference between a compiler and interpreter?\n\n"
"A: Compilers are only used for web pages.\n"
"B: Compilers translate all code at once; interpreters translate one line at a time.\n"
"C: Interpreters create executable files.\n"
"D: Compilers only work with images.\n"
};
// Size of entire array in bytes (~128 bytes, ~32 each)
// Divided by the size of 1 element
constexpr size_t amountOfElements = size(questions);
const string correctAnswers[] = {"B", "B", "B"};
// Limit the answers they can input
string acceptableInputs[4] = {"A", "B", "C", "D"};
const size_t acceptableListSize = size(acceptableInputs) / size(acceptableInputs[0]);
int score = 0;
// Methods
string makeUpper(string stringToConvert) {
for (char &c : stringToConvert) {
// Cast it to unsigned char first to avoid negative value issues (toupper expects this)
const int upper = toupper(static_cast<unsigned char>(c));
c = static_cast<char>(upper); // Convert each character to uppercase (back to a normal char)
}
return stringToConvert;
}
bool isValidAnswer(const size_t listSize, string acceptableInputs[], const string& userAnswer) {
for (int j=0; j < listSize; j++ ) {
if (acceptableInputs[j] == userAnswer) {
return true;
}
}
return false;
}
void quizLoop() {
// Asking the user the questions
for (size_t i=0; i < amountOfElements; i++ ) {
string userAnswer;
// Keep prompting until they give a valid input
while (true) {
// Ask the question
cout << questions[i] << endl;
// Get user input
cin >> userAnswer;
userAnswer = makeUpper(userAnswer);
if (isValidAnswer(acceptableListSize, acceptableInputs, userAnswer)) {
if (userAnswer == correctAnswers[i]) {
score++;
cout << "Correct!\n";
} else {
cout << "Wrong!\n";
}
break;
}
// Incase the entry wasn't valid
cout << "You entered an invalid answer. Please type A,B,C or D.\n" << endl;
}
}
cout << "Your final score is " << score << "." << endl;
}
int main() {
quizLoop();
// If the user wants to play again
string playAgainResponse;
while (true) {
cout << "Would you like to play again? (Y/N)" << endl;
string acceptablePlayAgainInputs[2] = {"Y", "N"};
cin >> playAgainResponse;
playAgainResponse = makeUpper(playAgainResponse);
if (isValidAnswer(size(acceptablePlayAgainInputs), acceptablePlayAgainInputs, playAgainResponse)) {
break;
} else {
cout << "Please enter Y or N.\n";
}
}
if (playAgainResponse == "Y") {
quizLoop();
}
else if (playAgainResponse == "N") {
cout << "Thanks for playing!";
}
return 0;
}