🌿 Smart Garden Manager in C++ with Robotics, UI, Drones, and Sound "A Rainwater Conservation System for Tomorrow’s Farms"

Here is another project on the C++ series. If you have been following along we have built a lot of projects on C++ till here which means you may have a nice practice of hands on C++ and making useful products out of it. So if you have missed the early projects then do check them out. Let's continue the series with this new mini quiz game.
This game asks multiple choice questions and takes answers as the input. Finally after calculation it displays the results.
Arrays.....
Are used for storing questions, answer options and the right answers
Loops...
For iterating through all questions
Conditional statements...
For evaluation of answers and calculation of scores.
#include <iostream>
#include <string>
using namespace std;
int main() {
// Quiz data
const int NUM_QUESTIONS = 3; // Number of questions
string questions[NUM_QUESTIONS] = {
"What is the capital of France?",
"Which programming language is known as the 'mother of all languages'?",
"What is 5 + 3?"
};
string options[NUM_QUESTIONS][4] = {
{"1. Berlin", "2. Madrid", "3. Paris", "4. Rome"},
{"1. Python", "2. Assembly", "3. C", "4. Java"},
{"1. 6", "2. 7", "3. 8", "4. 9"}
};
int correctAnswers[NUM_QUESTIONS] = {3, 3, 3}; // Correct options
int userAnswers[NUM_QUESTIONS]; // To store user input
int score = 0; // User's score
// Game starts
cout << "Welcome to the Mini Quiz Game!\n";
cout << "------------------------------\n";
cout << "Answer the questions by entering the option number (1-4).\n";
// Quiz loop
for (int i = 0; i < NUM_QUESTIONS; ++i) {
// Display question
cout << "\nQuestion " << i + 1 << ": " << questions[i] << endl;
// Display options
for (int j = 0; j < 4; ++j) {
cout << options[i][j] << endl;
}
// Take user's answer
cout << "Your answer: ";
cin >> userAnswers[i];
// Validate input
while (userAnswers[i] < 1 || userAnswers[i] > 4) {
cout << "Invalid choice. Please select an option between 1 and 4: ";
cin >> userAnswers[i];
}
// Check if the answer is correct
if (userAnswers[i] == correctAnswers[i]) {
cout << "Correct!\n";
++score; // Increase score
} else {
cout << "Wrong. The correct answer was option " << correctAnswers[i] << ".\n";
}
}
// Display final score
cout << "\nQuiz Over!\n";
cout << "Your final score: " << score << " out of " << NUM_QUESTIONS << endl;
return 0;
}
Setting up the quiz data...
const int NUM_QUESTIONS = 3;
string questions[NUM_QUESTIONS] = {
"What is the capital of France?",
"Which programming language is known as the 'mother of all languages'?",
"What is 5 + 3?"
};
string options[NUM_QUESTIONS][4] = {
{"1. Berlin", "2. Madrid", "3. Paris", "4. Rome"},
{"1. Python", "2. Assembly", "3. C", "4. Java"},
{"1. 6", "2. 7", "3. 8", "4. 9"}
};
int correctAnswers[NUM_QUESTIONS] = {3, 3, 3};
Questions...
Storing the text for every question:
Options...
Storing four choices for every question
CorrectAnswers...
Is an array in which every element is corresponding to the right option for every question.
Displaying all questions and the right answer options...
cout << "\nQuestion " << i + 1 << ": " << questions[i] << endl;
for (int j = 0; j < 4; ++j) {
cout << options[i][j] << endl;
}
Using a nested loop here for every question following its right answer
Input and its validation
cin >> userAnswers[i];
while (userAnswers[i] < 1 || userAnswers[i] > 4) {
cout << "Invalid choice. Please select an option between 1 and 4: ";
cin >> userAnswers[i];
}
Makes sure that answer of user is between 1 and 4
If user input is not valid the game asks the user to try again
Checking and the scores...
if (userAnswers[i] == correctAnswers[i]) {
cout << "Correct!\n";
++score;
} else {
cout << "Wrong. The correct answer was option " << correctAnswers[i] << ".\n";
}
Comparison of users answers to right options
If it finds the answers right , increases the scores
Displaying of final score....
cout << "Your final score: " << score << " out of " << NUM_QUESTIONS << endl;
Total final scores are displayed.
Now let's go through a full sample of the game.
....
Example of input and output
Welcome to the Mini Quiz Game!
------------------------------
Answer the questions by entering the option number (1-4).
Question 1: What is the capital of France?
1. Berlin
2. Madrid
3. Paris
4. Rome
Your answer: 3
Correct!
Question 2: Which programming language is known as the 'mother of all languages'?
1. Python
2. Assembly
3. C
4. Java
Your answer: 2
Wrong. The correct answer was option 3.
Question 3: What is 5 + 3?
1. 6
2. 7
3. 8
4. 9
Your answer: 3
Correct!
Quiz Over!
Your final score: 2 out of 3
So that was the game. I hope you like the idea and have learned something new from this blog. Thank you so much guys and I will see you next time!
Comments
Post a Comment