Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

what isa the problem of this code

+7 votes
asked May 23, 2025 by arjee bryant paragele (290 points)
#include <stdio.h>
#include <stdlib.h>

// Function declarations
void easyLEVEL(int *score);
void mediumLEVEL(int *score);
void hardLEVEL(int *score);

int main() {
    int level;
    int score;
    char tryAgain;
    char name[30];

    printf("Name of the player: ");
    scanf("%s", name);

    do {
        score = 0;  // Reset score at the beginning of each round

        printf("\nChoose your level:\n");
        printf("1. EASY\n2. MEDIUM\n3. HARD\n");
        printf("Enter level (1-3): ");
        scanf("%d", &level);

        switch (level) {
            case 1:
                easyLEVEL(&score);
                break;
            case 2:
                mediumLEVEL(&score);
                break;
            case 3:
                hardLEVEL(&score);
                break;
            default:
                printf("Invalid level! Please restart the quiz.\n");
                continue; // Go back to the start of the loop
        }

        printf("\n=== Quiz Complete ===\n");
        printf("Your Total Score: %d out of 3\n", score);

        if (score == 3)
            printf("Excellent! Perfect score!\n");
        else if (score == 2)
            printf("Good job! Almost perfect.\n");
        else if (score == 1)
            printf("You got 1 correct. Keep practicing.\n");
        else
            printf("No correct answers. Try again and review the basics.\n");

        printf("\nDo you want to try again? (Y/N): ");
        scanf(" %c", &tryAgain);

    } while (tryAgain == 'Y' || tryAgain == 'y');

    printf("\nTHANK YOU AND HAVE A GOOD DAY, MAAM/SIR :)\n");
    return 0;
}

void easyLEVEL(int *score) {
    char answer;

    printf("\n--- EASY LEVEL ---\n");

    printf("\n1. What does 'int' stand for in C?\n");
    printf("A. Integer\nB. Internet\nC. Input\nD. Internal\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'A' || answer == 'a') (*score)++;

    printf("\n2. Which symbol is used to end a statement in C?\n");
    printf("A. :\nB. ;\nC. .\nD. ,\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'B' || answer == 'b') (*score)++;

    printf("\n3. Which function is used to print in C?\n");
    printf("A. scanf\nB. input\nC. echo\nD. printf\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'D' || answer == 'd') (*score)++;
}

void mediumLEVEL(int *score) {
    char answer;

    printf("\n--- MEDIUM LEVEL ---\n");

    printf("\n1. What is the correct syntax to declare a pointer in C?\n");
    printf("A. int *ptr;\nB. int ptr*;\nC. int ptr;\nD. pointer int;\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'A' || answer == 'a') (*score)++;

    printf("\n2. Which loop runs at least once?\n");
    printf("A. for\nB. while\nC. do-while\nD. goto\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'C' || answer == 'c') (*score)++;

    printf("\n3. What does '&&' mean in C?\n");
    printf("A. OR\nB. NOT\nC. AND\nD. EQUAL\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'C' || answer == 'c') (*score)++;
}

void hardLEVEL(int *score) {
    char answer;

    printf("\n--- HARD LEVEL ---\n");

    printf("\n1. What is the output of printf(\"%%d\", 5 + 2 * 3); ?\n");
    printf("A. 21\nB. 11\nC. 13\nD. 17\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'B' || answer == 'b') (*score)++;

    printf("\n2. What does malloc() return if memory allocation fails?\n");
    printf("A. 0\nB. NULL\nC. -1\nD. Error\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'B' || answer == 'b') (*score)++;

    printf("\n3. Which data structure uses LIFO?\n");
    printf("A. Queue\nB. Stack\nC. Linked List\nD. Array\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'B' || answer == 'b') (*score)++;
}

1 Answer

+1 vote
answered May 27, 2025 by Er. Gangaram (280 points)
#include <stdio.h>
#include <stdlib.h>

// Function declarations
void easyLEVEL(int *score);
void mediumLEVEL(int *score);
void hardLEVEL(int *score);

int main() {
    int level;
    int score;
    char tryAgain;
    char name[30];

    printf("Name of the player: ");
    scanf(" %[^\n]%*c", name);  // Now supports full name with spaces

    do {
        score = 0;  // Reset score at the beginning of each round

        printf("\nChoose your level:\n");
        printf("1. EASY\n2. MEDIUM\n3. HARD\n");
        printf("Enter level (1-3): ");
        scanf("%d", &level)

        switch (level) {
            case 1:
                easyLEVEL(&score);
                break;
            case 2:
                mediumLEVEL(&score);
                break;
            case 3:
                hardLEVEL(&score);
                break;
            default:
                printf("Invalid level! Please restart the quiz.\n");
                continue;
        }

        printf("\n=== Quiz Complete ===\n");
        printf("Your Total Score: %d out of 3\n", score);

        if (score == 3)
            printf("Excellent! Perfect score!\n");
        else if (score == 2)
            printf("Good job! Almost perfect.\n");
        else if (score == 1)
            printf("You got 1 correct. Keep practicing.\n");
        else
            printf("No correct answers. Try again and review the basics.\n");

        printf("\nDo you want to try again? (Y/N): ");
        scanf(" %c", &tryAgain);

    } while (tryAgain == 'Y' || tryAgain == 'y');

    printf("\nTHANK YOU AND HAVE A GOOD DAY, MAAM/SIR :)\n");
    return 0;
}

void easyLEVEL(int *score) {
    char answer;

    printf("\n--- EASY LEVEL ---\n");

    printf("\n1. What does 'int' stand for in C?\n");
    printf("A. Integer\nB. Internet\nC. Input\nD. Internal\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'A' || answer == 'a') (*score)++;

    printf("\n2. Which symbol is used to end a statement in C?\n");
    printf("A. :\nB. ;\nC. .\nD. ,\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'B' || answer == 'b') (*score)++;

    printf("\n3. Which function is used to print in C?\n");
    printf("A. scanf\nB. input\nC. echo\nD. printf\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'D' || answer == 'd') (*score)++;
}

void mediumLEVEL(int *score) {
    char answer;

    printf("\n--- MEDIUM LEVEL ---\n");

    printf("\n1. What is the correct syntax to declare a pointer in C?\n");
    printf("A. int *ptr;\nB. int ptr*;\nC. int ptr;\nD. pointer int;\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'A' || answer == 'a') (*score)++;

    printf("\n2. Which loop runs at least once?\n");
    printf("A. for\nB. while\nC. do-while\nD. goto\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'C' || answer == 'c') (*score)++;

    printf("\n3. What does '&&' mean in C?\n");
    printf("A. OR\nB. NOT\nC. AND\nD. EQUAL\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'C' || answer == 'c') (*score)++;
}

void hardLEVEL(int *score) {
    char answer;

    printf("\n--- HARD LEVEL ---\n");

    printf("\n1. What is the output of printf(\"%%d\", 5 + 2 * 3); ?\n");
    printf("A. 21\nB. 11\nC. 13\nD. 17\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'B' || answer == 'b') (*score)++;

    printf("\n2. What does malloc() return if memory allocation fails?\n");
    printf("A. 0\nB. NULL\nC. -1\nD. Error\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'B' || answer == 'b') (*score)++;

    printf("\n3. Which data structure uses LIFO?\n");
    printf("A. Queue\nB. Stack\nC. Linked List\nD. Array\n");
    printf("Your answer: ");
    scanf(" %c", &answer);
    if (answer == 'B' || answer == 'b') (*score)++;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...