168: Quizz Game in C++
#include <iostream>
#include <iomanip>
using namespace std;
bool isValid(int x) // checking if the option choosed by user is valid or not
{
cin.clear(); // this is done so that program don't become a mess
fflush(stdin); // when user enter some string while choosing option
return (x == 1 || x == 2 || x == 3 || x == 4);
}
int main()
{
string questions[] = {"Choose the capital of France:",
"Which planet is closest to The Sun:",
"Which one of the following is the primary Color:",
"How many legs do spider has:"};
string options[][4] = {{"Madrid", "London", "Rome", "Paris"},
{"Venus", "Mars", "Mercury", "Earth"},
{"Blue", "Orange", "Purple", "Green"},
{"6", "8", "10", "14"}};
int answer[] = {4, 3, 1, 2}, choice;
int correct = 0, incorrect = 0;
int i = 0; // this is written outside because we want to run the loop again
// when everything outside the loop is also executed (before this for loop)
for (string element : questions)
{
cout << "____________________________________________________\n";
cout << "| " << setw(49) << left << element << "|\n";
cout << "****************************************************\n";
for (; i < 4;)
{
for (int j = 0; j < 4; j++)
{
cout << "| " << j + 1 << ": " << setw(46) << left << options[i][j] << "|\n";
}
cout << "****************************************************\n";
cout << "Enter your choice: ";
cin >> choice;
while (!isValid(choice))
{
cout << "Invalid Choice (1, 2, 3, 4) only\n";
cout << "Enter your choice: ";
cin >> choice;
}
if (choice == answer[i])
{
cout << "Correct\n\n";
correct++;
}
else
{
cout << "Incorrect\n";
incorrect++;
cout << "Correct: " << options[i][answer[i] - 1] << endl << endl;
// in correct, row of option is i and column is answer at i minus 1
// because answer is starting from 1 and we need index that starts
// from 0;
}
i++;
break;
}
}
cout << endl
<< "Correct: " << correct << endl;
cout << "Incorrect: " << incorrect << endl;
cout << endl
<< "Marks: " << correct << '/' << incorrect + correct << endl;
cout << "Percenctage: " << ((float)correct / (correct + incorrect)) * 100 << '%' << endl;
return 0;
}
Comments
Post a Comment