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

Popular posts from this blog

153: Write a program to read an amount (integer value) and break the amount into smallest possible number of bank notes. Note: The possible banknotes are 500, 100, 50, 20, 10, 5, 2, and 1

206: Write a program to create a class named "Circle" which has the property "radius". Define functions to calculate the area and circumference of the circle.

221: // In Task 2, we discussed multilevel inheritance with parameterized constructors for Student, UndergraduateStudent, and GraduateStudent classes in a university management system. Can you explain the advantages of using multilevel inheritance with specific details about the functions and data members in these classes? How were the parameterized constructors (e.g., setting student name, age, and ID) used to ensure that each class in the hierarchy correctly initializes its properties, such as creating an UndergraduateStudent named "John," aged 20, with a student ID of 12345