Open In App

Quiz Game in C

Last Updated : 16 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Quiz Games are one of the most interactive and mind blogging games of all time. Whether it be a child or teen or an adult, these Quiz Games fascinate people of all age groups. In this article, we will make a simple Quiz Game using C programming language.

Functionalities of a Quiz Game

The Quiz Game program provides the following functionalities:

  • Store Questions
  • Randomly Pick One Question while playing
  • Check Answers if they are correct/wrong
  • Get the Final Score

Prerequisites: Basic knowledge of C, Control Statements, Arrays, Strings, Functions and Structures.

quiz game in c

Components of the Quiz Game Program

The Quiz Game program is made up of the following major components:

1. Header Files and Macro Definitions

  • Include all the necessary header files that you are going to use in your program: Here we are using <stdio.h>, <stdlib.h>, <string.h>, and <time.h>
  • Use the define keyword to declare constants that you’ll be using throughout your program. Here a constant “MAX_QUESTIONS” is defined to specify the maximum number of questions in the quiz. We are declaring MAX_QUESTIONS as 5 since we’ll be having 5 questions in total for the quiz.

2. Question Structure

A structure named “Question” is defined to represent a quiz question containing the following fields:

  • question: It is declared as a string to store the question text.
  • options: It is a 2D array of strings to store the multiple-choice options.
  • correct_option: It is declared as an integer to store the index of the correct option (1-4).

3. displayQuestion() Function

  • This displayQuestion() function is defined for the purpose of displaying a quiz question. It takes a Question structure as an argument and prints the question text along with the answer options.

4. checkAnswer() Function

  • The checkAnswer() function is used to check whether a user’s answer is correct or not. It also takes a Question structure and the user’s answer as arguments and returns 1 if the answer is correct or returns 0 if the answer is incorrect.

5. main() Function

  • The main() function marks the beginning of the program. It means from here the program execution starts.
  • It initializes a random number generator using srand() based on the current time which is taken from <stdlib.h> header file.
  • An array of “original_questions” is defined which contains the quiz questions and their correct answers.

6. Randomized Questions

  • A copy of the original questions array (“original_questions”) is created in the “questions” array. This copy will be used to randomize the order of questions.
  • The variable “num_questions” is set to “MAX_QUESTIONS”, representing the total number of questions available.

Execution Flow of the Program

  1. The program greets the user and then enters a loop to ask questions.
  2. In each iteration of the loop:
    1. A random index is generated to select a question from the remaining pool of questions. Since we have used srand as discussed earlier.
    2. The selected question is displayed using the “displayQuestion” function.
    3. The user is prompted to enter their answer (1-4).
    4. The program checks if the answer is valid and within the range 1-4.
    5. If the answer is valid, it checks if it’s correct using the “checkAnswer” function and updates the user’s score accordingly.
    6. If the answer is incorrect, it displays the correct answer.
    7. If the answer is invalid, it prompts the user to enter a valid choice.
    8. The selected question is removed from the “questions” array by replacing it with the last question in the array, and “num_questions” is decremented.
  3. After all questions have been asked, the program displays the user’s score with a message of Well-Done Champ.
  4. The program exits, ending the quiz game.

Overall, this program is determined to conduct a randomized quiz game with a fixed set of questions and multiple-choice answers to those questions. It provides feedback to the user on whether their answers are correct and keeps track of the score. The user’s score is displayed at the end of the quiz with a message.

C Program to Implement Quiz Game

C




// C program to implement a simple quiz game
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
 
// max number of questions defined as macro
#define MAX_QUESTIONS 5
 
// Structure to store question details
typedef struct {
    char question[256];
    char options[4][64];
    int correct_option;
} Question;
 
// function to display question to the user
void displayQuestion(Question q)
{
    printf("%s\n", q.question);
    for (int i = 0; i < 4; i++) {
        printf("%d. %s\n", i + 1, q.options[i]);
    }
}
 
// function to check the answer
int checkAnswer(Question q, int user_answer)
{
    return (user_answer == q.correct_option);
}
 
// driver code
int main()
{
 
    // random number generator
    srand(time(NULL));
 
    // Initializing questions, options and the correct
    // answer
    Question original_questions[MAX_QUESTIONS] = {
        { "Which bird lays the largest egg?",
          { "Owl", "Ostrich", "Kingfisher", "Woodpecker" },
          2 },
        { "How many legs does a spider have?",
          { "7", "8", "6", "5" },
          2 },
        { "Where does the President of the United States "
          "live while in office?",
          { "The White House", "The Parliament",
            "House of Commons", "Washington DC" },
          1 },
        { "Which state is famous for Hollywood?",
          { "Sydney", "California", "New York", "Paris" },
          2 },
        { "What is a group of lions called?",
          { "Drift", "Pride", "Flock", "Drove" },
          2 }
    };
 
    // Array of struct data-type
    Question questions[MAX_QUESTIONS];
    memcpy(questions, original_questions,
           sizeof(original_questions));
 
    int num_questions = MAX_QUESTIONS;
 
    int score = 0;
 
    printf("Hola! Here's your Quiz Game!\n");
 
    for (int i = 0; i < MAX_QUESTIONS; i++) {
        int random_index = rand() % num_questions;
        Question current_question = questions[random_index];
        displayQuestion(current_question);
 
        int user_answer;
        printf("Enter your answer (1-4): ");
        scanf("%d", &user_answer);
 
        if (user_answer >= 1 && user_answer <= 4) {
            if (checkAnswer(current_question,
                            user_answer)) {
                printf("Correct!\n");
                score++;
            }
            else {
                printf("Incorrect. The correct answer is "
                       "%d. %s\n",
                       current_question.correct_option,
                       current_question.options
                           [current_question.correct_option
                            - 1]);
            }
        }
        else {
            printf("Invalid choice. Please enter a number "
                   "between 1 and 4.\n");
        }
 
        questions[random_index]
            = questions[num_questions - 1];
        num_questions--;
    }
 
    printf("Well Done Champ !!!! Quiz completed! Your "
           "score: %d/%d\n",
           score, MAX_QUESTIONS);
 
    return 0;
}


Output



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads