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

88: Using switch statement Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: // Percentage >= 90% : Grade A Percentage >= 80% : Grade B Percentage >= 70% : Grade C Percentage >= 60% : Grade D Percentage >= 40% : Grade E Percentage < 40% : Grade F

205: Book Catalog: Define a struct to represent a book with attributes like title, author, and publication year. Write a program to create a catalog of books by taking user input and display books published after a certain year.

15: Take input of age and name of 3 people by user and determine oldest and youngest among them with his age. -_-_-_-_-_-_-_-_-(line with spaces input concept)-_-_-_-_-_-_-_-_