-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
283 lines (246 loc) · 8 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
280
281
282
283
//Filename: main.cpp
//Author: Sachith Wickramarachchi
//Assignment Number : 2
//Program for a multiple choice question quiz and determine score
//Last Changed: April 9th, 2018
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
int total_score;
int option_number;
int getMenuOption();
int exitProgram();
void quiz();
void readFile();
int main()
{
option_number = getMenuOption();
switch(option_number) {
case 1:
quiz();
cout << endl;
main();
return 0;
case 2:
readFile();
main();
return 0;
case 3:
exitProgram();
return 0;
break;
default:
cout << "Invalid Input. Please enter the correct option number" << endl;
main();
return 0;
}
}
int getMenuOption()
{
cout << endl;
cout << "*********************** Green Energy Facts Quiz MENUv1.0 *************************" << endl;
cout << endl;
cout << " 1. Quiz " << endl;
cout << " 2. View Score " << endl;
cout << " 3. Quit " << endl;
cout << endl;
cout << "**********************************************************************************" << endl;
cout << "Enter an option number." << endl;
int user_option;
cout << "Option: ";
cin >> user_option;
return(user_option);
}
int exitProgram()
{
cout << "Program Ended" << endl;
return 0;
}
void quiz() {
total_score = 0;
char user_name[30], q1_answer, q2_answer, q3_answer, q4_answer, q5_answer, q6_answer, q7_answer, q8_answer, q9_answer, q10_answer;
ofstream myfile;
cout << "Please enter your name: ";
cin >> user_name;
cout << endl;
cout << "Hi " << user_name << ", " << "Welcome to the Green Energy Quiz!" << endl;
cout << "Enter the answers in the form of a, b, c or d." << endl;
cout << endl;
cout << "Press enter to start the quiz." << endl;
cin.get();
//Question 1
cout << "Q1: Which country produces most of Hydroelectric energy?" << endl;
cout << "a. Canada.\n";
cout << "b. China.\n";
cout << "c. Sri Lanka.\n";
cout << "d. Italy.\n";
cout << endl;
cout << "Enter your answer" << endl;
cin >> q1_answer;
cout << endl;
if(q1_answer == 'b') {
total_score += 1;
}
//Question 2
cout << "Q2: How much renewable energy does the world generate approximately?" << endl;
cout << "a. 4,000 billion kWh.\n";
cout << "b. 50,000 billion kWh.\n";
cout << "c. 500,000 billion kWh.\n";
cout << "d. 6,800,000 billion kWh.\n";
cout << endl;
cout << "Enter your answer" << endl;
cin >> q2_answer;
cout << endl;
if(q2_answer == 'a') {
total_score += 1;
}
//Question 3
cout << "Q3: How many types of renewable energy are there normally said to be?" << endl;
cout << "a. 10.\n";
cout << "b. 5.\n";
cout << "c. 3.\n";
cout << "d. 7.\n";
cout << endl;
cout << "Enter your answer" << endl;
cin >> q3_answer;
cout << endl;
if(q3_answer == 'd') {
total_score += 1;
}
//Question 4
cout << "Q4: What is the oldest known use of renewable energy?" << endl;
cout << "a. In 200 BC people in China and the Middle East used windmills to pump water and grind grain.\n";
cout << "b. In 500BC people in England used windmills to plant grain.\n";
cout << "c. In 1800 windmills in India were used to pump water for salt making.\n";
cout << "d. In South Africa, 1.8m BC biomass was used as fuel for fire.\n";
cout << endl;
cout << "Enter your answer" << endl;
cin >> q4_answer;
cout << endl;
if(q4_answer == 'd') {
total_score += 1;
}
//Question 5
cout << "Q5: Which country developed the first windmill?" << endl;
cout << "a. China.\n";
cout << "b. Nigeria.\n";
cout << "c. Japan.\n";
cout << "d. Kuwait.\n";
cout << endl;
cout << "Enter your answer" << endl;
cin >> q5_answer;
cout << endl;
if(q5_answer == 'a') {
total_score += 1;
}
//Question 6
cout << "Q6: Which of the following is not a renewable?" << endl;
cout << "a. A backpack that produces energy from walking.\n";
cout << "b. A knee brace that harvests kinetic energy.\n";
cout << "c. A t-shirt battery charged by solar power.\n";
cout << "d. A gym powered by its runners.\n";
cout << endl;
cout << "Enter your answer" << endl;
cin >> q6_answer;
cout << endl;
if(q6_answer == 'c') {
total_score += 1;
}
//Question 7
cout << "Q7: Which of these countries have NOT vowed to use only 100% renewable energy by 2050?" << endl;
cout << "a. Afghanistan.\n";
cout << "b. Canada.\n";
cout << "c. Guatemala.\n";
cout << "d. Vietnam.\n";
cout << endl;
cout << "Enter your answer" << endl;
cin >> q7_answer;
cout << endl;
if(q7_answer == 'b') {
total_score += 1;
}
//Question 8
cout << "Q8: Which of the following is not considered to be a source of renewable energy?" << endl;
cout << "a. Hydropower.\n";
cout << "b. Wind.\n";
cout << "c. Natural gas.\n";
cout << "d. Solar.\n";
cout << endl;
cout << "Enter your answer" << endl;
cin >> q8_answer;
cout << endl;
if(q8_answer == 'c') {
total_score += 1;
}
//Question 9
cout << "Q9: How many homes can a single 1MW wind turbine power?" << endl;
cout << "a. 10.\n";
cout << "b. 120.\n";
cout << "c. 200.\n";
cout << "d. 300.\n";
cout << endl;
cout << "Enter your answer" << endl;
cin >> q9_answer;
cout << endl;
if(q9_answer == 'd') {
total_score += 1;
}
//Question 10
cout << "Q10: Which of the following gasses can be generated and used for cooking and lighting in rural areas?" << endl;
cout << "a. Biogas.\n";
cout << "b. Oxygen.\n";
cout << "c. Ammonia.\n";
cout << "d. None of these.\n";
cout << endl;
cout << "Enter your answer" << endl;
cin >> q10_answer;
cout << endl;
if(q10_answer == 'a') {
total_score += 1;
}
//Feedback
cout << "The quiz is over. Here is your feedback." << endl;
cout << endl;
if(total_score==10) {
cout << "Result: Excellent!\n";
} else if(total_score<10 & total_score>=6) {
cout << "Result: Very Good!\n";
} else if(total_score<=5) {
cout << "Result: Time to brush up on your knowledge of global warming." << endl;
cout << "You could refer these websites to learn more." << endl;
cout << endl;
cout << "1. https://www.mnn.com/earth-matters/energy/stories/what-is-green-energy" << endl;
cout << "2. https://www.sporcle.com/games/PumpkinBomb/energy" << endl;
cout << "3. http://www.renewableenergyworld.com/index/tech.html" << endl;
cout << "4. https://en.wikipedia.org/wiki/Renewable_energy" << endl;
cout << endl;
}
//Write results to a text file
myfile.open("/Users/ViduniUsh/Desktop/MyDocs/Projects/Quiz/Quiz/results.txt", ios::out);
if(myfile.is_open())
{
myfile << "Name of participant: ";
myfile << user_name << endl;
myfile << "Quiz Results: " << total_score << endl;
myfile.close();
} else
cout << "Unable to open file";
}
//Function to read contents of the text file when option 2 is chosen
void readFile()
{
string line;
ifstream myfile("/Users/ViduniUsh/Desktop/MyDocs/Projects/Quiz/Quiz/results.txt");
if(myfile.is_open())
{
while(getline(myfile, line))
{
cout << endl;
cout << line << endl;
}
myfile.close();
} else
cout << "Unable to open file" << endl;
}