Home > Net >  C When the lives counter in the while loop is met, it still doesn't exit the loop and goes to a
C When the lives counter in the while loop is met, it still doesn't exit the loop and goes to a

Time:10-11

Still a beginner and a first year student. I am trying to create a simple quiz game as a practice, but when I try to create a counter that serves as the remaining chances and it diminishes every time the user answers incorrectly. The problem is the loop does not exit when the lives counter reaches 0, rather it goes to a negative value and it does not exit the loop. I still don't know if I used the while loop correctly. I hope to get a simple answer. Thanks!

#include<stdio.h>

int main (void) {

    //quiz variables
    int gamestart,ingame=1;
    int scr1,scr2,scr3,scr4,scr5;
    int ans1,ans2,ans3,ans4,ans5;
    char name[25];
    int total=0;
    //start of quiz
    printf("--------Hello welcome to the quiz game!--------\n");
    printf("----Press 1 to start the game and 0 to quit----\n");
    scanf("%d",&gamestart);

    //quiz code
    int lives=3;
    while (ingame==1 && lives>0 && lives<=3){
    if (gamestart==1){
        
        printf("Enter your first name: ");
        scanf("%s",&name);
        printf("Hello %s! Let's start the game...\n",name);
        
        printf("First Question: What does “www” stand for in a website browser?\n");
        printf("1. Whole Wide World    2. Whole Wide Web\n");
        printf("3. World Wide Web      4. World Wide Whole\n");
        
        printf("Enter the number of your answer: ");
        scanf("%d",&ans1);
        switch(ans1){
        case 1:
            printf("Wrong Answer. You gain 0 points");
            scr1=0;
            break;
        case 2:
            printf("Wrong Answer. You gain 0 points");
            scr1=0;
            break;
        case 3:
            printf("Correct Answer! You gain 5 points!");
            scr1=5;
            break;
        default:
            printf("Wrong Answer. You gain 0 points");
            scr1=0;
            break;
        }
        total  = scr1;
        if (scr1 ==0 )
            lives--;
        printf("\nLives left: %d",lives);
        printf("\nYour total score this round is %d ",total);
        printf("\n");
        printf("\nNext Question...\n");
        printf("What is \"cynophobia\"?\n");
        printf("1. Fear of dogs        2. Fear of cats\n");
        printf("3. Fear of cyanide     4. Fear of the color blue\n");
        printf("Enter the number of your answer: ");
        scanf("%d",&ans2);
        switch(ans2){
        case 1:
            printf("Correct Answer! You gain 5 points!");
            scr2=5;
            break;
        case 2:
            printf("Wrong Answer. You gain 0 points");
            scr2=0;
            break;
        case 3:
            printf("Wrong Answer. You gain 0 points");
            scr2=0;
            break;
        default:
            printf("Wrong Answer. You gain 0 points");
            scr2=0;
            break;
        }
        total =scr2;
        if (scr2==0)
            lives--;
        printf("\nLives left: %d",lives);
        printf("\nYour total score this round is %d ",total);
        ingame=0;
    } else if (gamestart == 0) {
          ingame=0;
    }else{
     printf("Invalid Keyword\n");
     printf("----Press 1 to start and 0 to quit----\n");
     scanf("%d",&gamestart);
     }
     printf("\n\n\nThank you!");
     return 0;
}

CodePudding user response:

Firstly, I don't see the reason you are using all of these switch statements, just use if. Also format your code probably so we can read it.

Here is my take on your code:


#include <stdio.h>

int main(void)
{

    // quiz variables
    int gamestart, ingame = 1;
    int total = 0;
    int ans;
    char name[25];

    // start of quiz
    printf("--------Hello welcome to the quiz game!--------\n");
    printf("----Press 1 to start the game and 0 to quit----\n");
    scanf("%d", &gamestart);

    // quiz code
    int lives = 3;

    while (ingame == 1 && lives > 0)
    {
        if (gamestart == 1)
        {

            printf("Enter your first name: ");
            scanf("%s", &name);
            printf("Hello %s! Let's start the game...\n", name);

            printf("First Question: What does “www” stand for in a website browser?\n");
            printf("1. Whole Wide World    2. Whole Wide Web\n");
            printf("3. World Wide Web      4. World Wide Whole\n");

            printf("Enter the number of your answer: ");
            scanf("%d", &ans);
            if (ans == 3)
            {
                printf("Correct Answer! you gain 5 points!");
                total  = 5;
            }
            else
            {
                printf("Wrong Answer! You gain 0 points!");
                lives--;

                if (lives == 0)
                {
                    printf("You lost");
                    break; // or do something else
                }
            }

            printf("\nLives left: %d", lives);
            printf("\nYour total score this round is %d ", total);
            printf("\n");
            printf("\nNext Question...\n");
            printf("What is \"cynophobia\"?\n");
            printf("1. Fear of dogs        2. Fear of cats\n");
            printf("3. Fear of cyanide     4. Fear of the color blue\n");
            printf("Enter the number of your answer: ");

            scanf("%d", &ans);
            if (ans == 1)
            {
                printf("Correct Answer! you gain 5 points!");
                total  = 5;
            }
            else
            {
                printf("Wrong Answer! You gain 0 points!");
                lives--;

                if (lives == 0)
                {
                    printf("You lost");
                    break; // or do something else
                }
            }

            printf("\nLives left: %d", lives);
            printf("\nYour total score this round is %d ", total);
            printf("\n");
            printf("\nNext Question...\n");
            printf("Who named the Pacific Ocean?\n");
            printf("1. Ferdinand Magellan        2. Christopher Colombus\n");
            printf("3. Miguel Lopez de Legazpi   4. Antonio Pigafetta\n");
            printf("Enter the number of your answer: ");
            scanf("%d", &ans);
            if (ans == 1)
            {
                printf("Correct Answer! you gain 5 points!");
                total  = 5;
            }
            else
            {
                printf("Wrong Answer! You gain 0 points!");
                lives--;
            }

            printf("\nLives left: %d", lives);
            printf("\nYour total score this round is %d ", total);
            printf("\n");
            printf("\nNext Question...\n");
            printf("What was the first soft drink in space?\n");
            printf("1. Fanta             2. Pepsi\n");
            printf("3. Coca-Cola         4. Dr. Pepper\n");
            printf("Enter the number of your answer: ");
            scanf("%d", &ans);
            if (ans == 3)
            {
                printf("Correct Answer! you gain 5 points!");
                total  = 5;
            }
            else
            {
                printf("Wrong Answer! You gain 0 points!");
                lives--;

                if (lives == 0)
                {
                    printf("You lost");
                    break; // or do something else
                }
            }

            printf("\nLives left: %d", lives);
            printf("\nYour total score this round is %d ", total);
            printf("\n");
            printf("\n");
            printf("Next Question...");
            printf("Which country invented ice cream?\n");
            printf("1. USA             2. China \n");
            printf("3. Spain           4. Turkey\n");
            printf("Enter the number of your answer: ");
            scanf("%d", &ans);
            if (ans == 2)
            {
                printf("Correct Answer! you gain 5 points!");
                total  = 5;
            }
            else
            {
                printf("Wrong Answer! You gain 0 points!");
                lives--;

                if (lives == 0)
                {
                    printf("You lost");
                    break; // or do something else
                }
            }

            printf("\nLives left: %d", lives);
            printf("\n");
            printf("\n");
            printf("\nYour total score in this game is %d \n", total);

            switch (total)
            {

            case 0:
                printf("Too bad!");
                break;
            case 5:
                printf("Try harder!");
                break;
            case 10:
                printf("Fair enough");
                break;
            case 15:
                printf("Pretty good!");
                break;
            default:
                printf("Excellent! Genius!");
                break;
            }
            ingame = 0;
        }
        else if (gamestart == 0)
        {
            ingame = 0;
        }
        else
        {
            printf("Invalid Keyword");
            printf("\n");
            printf("----Press 1 to start the game and 0 to quit----\n");
            scanf("%d", &gamestart);
        }
    }
    printf("\n");
    printf("\n");
    printf("\nThank you!");
    return 0;
}

I didn't add newlines. Also, I just did break when the live count is 0. You can do whatever you want with that.

CodePudding user response:

From my understanding, your "counter" is the variable lives and in your question you said it can go to negative. This is because you forgot to check whether lives == 0, therefore making it unused. After each question, you should check if lives == 0 and add some details (I'll leave it here for your creativity).

Edit: although it is up to your preference, I recommend shortening the code as much as possible (so that it doesn't go messy when it has errors), then add details after it works.

CodePudding user response:

This is rough, but it's an attempt to demonstrate how to write less code. This version still has your original 5 Q&As in it. It is still "rough around the edges" but I hope it provides you with some insights to write less-but-more-effective code...

#include <stdio.h>

int qa( int ans, char *str ) {
    int guess = 0;

    puts( str );
    printf("Your answer: ");
    scanf("%d", &guess);
    printf("Correct answer is %d! ", ans);
    printf("You gain %d points!\n\n", (guess == ans) * 5 );
    return guess == ans;
}

int main() {
    int res = 0, total = 0, lives = 3;

    for( int qno = 1; qno <= 5 && lives > 0; qno   ) {
        if(qno == 1)
            res = qa( 3,
                "What does 'WWW' stand for in a website browser?\n"
                "1. Whole Wide World    2. Whole Wide Web\n"
                "3. World Wide Web      4. World Wide Whole\n" );
        else if(qno == 2)
            res = qa( 1,
                "What is \"cynophobia\"?\n"
                "1. Fear of dogs        2. Fear of cats\n"
                "3. Fear of cyanide     4. Fear of the color blue\n" );
        else if(qno == 3)
            res = qa( 1,
                "Who named the Pacific Ocean?\n"
                "1. Ferdinand Magellan        2. Christopher Colombus\n"
                "3. Miguel Lopez de Legazpi   4. Antonio Pigafetta\n" );
        else if(qno == 4)
            res = qa( 3,
                "What was the first soft drink in space?\n"
                "1. Fanta             2. Pepsi\n"
                "3. Coca-Cola         4. Dr. Pepper\n" );
        else if(qno == 5)
            res = qa( 2,
                "Which country invented ice cream?\n"
                "1. USA             2. China \n"
                "3. Spain           4. Turkey\n" );

        if( res == 1 )
            total  = 5;
        else
            lives -= 1;

        printf("Your score is %d with %d lives left\n\n", total, lives);

        if(lives == 0)
            puts("Lives gone. You lost.");
    }

    char *cmnt = "Excellent! Genius!";
    switch (total) {
        case 0: cmnt = "Too bad!"; break;
        case 5: cmnt = "Try harder!"; break;
        case 10: cmnt = "Fair enough"; break;
        case 15: cmnt = "Pretty good!"; break;
    }
    puts(cmnt);

    return 0;
}
  • Related