简体   繁体   English

程序在输入“1”后简单地结束

[英]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"我正在用 C 语言制作一个基于文本的 RPG 游戏,但是我遇到了一个错误,VS 代码不报告主要是因为它实际上不是错误,所以基本上如果你首先看一下代码遇到功能它有一个是或否检查,但在输入 1(代表是)或 0(代表否)后,不会打印一条消息说“你逃跑”或“你不逃跑,死了”

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.像这样错误地使用scanf会导致未定义的行为,例如您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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