简体   繁体   中英

the program simply ends after inputting '1'

I am making a text based RPG game in C , but I am running into a bug , VS code doesn't report mainly because of the fact that it actually isn't an error , so basically if you look at the code in the first encounter function it has a yes or no check but after inputting 1 (standing for yes) or 0 (standing for no) a message is not printed saying "you run away" or "you do not run away , and die"

Please send help. also here's all the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>

int yes_or_no;
int alive = 1;

void begin()
{
    //introduction of the game
    printf("Welcome to a terminal based rpg! REMEMBER: you can't save so this is a rougelike and a death means that you have to start from the beginning\n");
    printf("Just a small project made during a summer vacation");
    printf("Now , you start your journey!\n");

    //game starts
    printf("As you head out on your way , you suddenly see...\n");
}

void first_encounter()
{
    //1st enemy encounter
    //NOTE:all enemy encounters after the first 5 will be random
    printf("A goblin!\n");
    printf("what do you do?!\n");
    printf("You don't even have a single weapon! Maybe you can run away...\n");

    /*NOTE:need to fix
    REASON:1 or 0 isn't a good choice to make choices in a game about making choices
    ---------------------*/
    printf("Will you run away? (1(standing for yes)/0(standing for no))\n");
    /*---------------------*/
    
    //first yes or no question , the main mechanic of the game
    scanf("%d" , yes_or_no);

    //checks the yes or no
    //if yes the game continues
    if (yes_or_no == 1)
    {
        printf("you run away");
    }

    //if no you are dead
    else if (yes_or_no == 0)
    {
        printf("you do not run away , and die");
        alive = 0;
        if (alive == 0)
        {
            exit(0);
        }
    }
}

int main(int argc, char const *argv[])
{ 
    begin();
    first_encounter();
    
    return 0;
}

Replace

scanf("%d" , yes_or_no);

With

scanf("%d" , &yes_or_no);

Using scanf incorrectly like that will cause undefined behaviour, such as what is going on in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM