C++ 如何修复此错误?

C++ 如何修复此错误?,c++,console-application,C++,Console Application,我正在申请玩龙与地下城,只是为了好玩,我遇到了一个问题。我已经写了多个printfs,但不知怎的它停止了工作。当它完成第一次计算并且必须进行下一次计算时,它就会停止。我不知道为什么 代码如下: #include <stdio.h> #include <stdlib.h> #include <ctime> #include <string> int main(){ //variables int dmgDice; char

我正在申请玩龙与地下城,只是为了好玩,我遇到了一个问题。我已经写了多个printfs,但不知怎的它停止了工作。当它完成第一次计算并且必须进行下一次计算时,它就会停止。我不知道为什么

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <string>

int main(){

    //variables
    int dmgDice;
    char entityName, dmgType;
    int initiative, dmgBonus, damage, userAC, userHp;

    //user input
    printf("Type in your armor class: \n");
    scanf("%d", &userAC);

    printf("Type in your hit points : \n");
    scanf("%d", &userHp);

    printf("type in your initative: \n");
    scanf("%d", &initiative);

    printf("type in you damage bonus: \n");
    scanf("%d", &dmgBonus);

    printf("type in you damage type: \n");
    scanf("%s", &dmgType);

    printf("your damage dice: \n");
    scanf("%d", &dmgDice);

    printf("Entity you want to damage: \n");
    scanf("%s", &entityName);

    //d20 roll
    srand((unsigned)time(0));

    printf("d20 roll: \n");
    int d20roll = (rand() % 20);
    int SUM = d20roll + initiative;
    printf("you have rolled SUM %d + %d = %d", d20roll, initiative, SUM);

    if(d20roll > 14){
        printf("\nYou've passed the enemies AC, now roll for damage!");
        printf("\nYou did %d damage to the %s!", damage, entityName);
    }
    else{
        printf("\nYou have missed, now %s attacks..", entityName);
        int entityd20roll = (rand() % 20) + 1;
        printf("\n%s passed your armor class, now it will roll for damage");
        if(entityd20roll > userAC){
            int edmg = (rand() % 12);
            int hp = userHp - edmg;
            if(hp < 0)
                hp *= -1;
            printf("\nhit points taken: \n");
            printf("%s has dealt %d damge", entityName, edmg);
        }
        else{
            printf("%s has missed you", entityName);
        }
    }


    return 0;
}
#包括
#包括
#包括
#包括
int main(){
//变数
int dmgDice;
字符实体名,dmgType;
int initiative、dmgBonus、DAMARY、userAC、userHp;
//用户输入
printf(“输入您的装甲等级:\n”);
scanf(“%d”、&userAC);
printf(“输入您的生命值:\n”);
scanf(“%d”、&userHp);
printf(“输入您的初始值:\n”);
scanf(“%d”和倡议);
printf(“输入你的伤害加成:\n”);
scanf(“%d”和dmgBonus);
printf(“输入损坏类型:\n”);
scanf(“%s”和&dmgType);
printf(“你的伤害骰子:\n”);
scanf(“%d”和dmgDice);
printf(“要损坏的实体:\n”);
scanf(“%s”、&entityName);
//d20辊
srand((未签名)时间(0));
printf(“d20卷:\n”);
int d20roll=(rand()%20);
整数和=d20roll+主动权;
printf(“您已滚动总和%d+%d=%d”,D20滚动,倡议,总和);
如果(D20滚动>14){
printf(“\n您已通过敌人AC,现在掷骰造成伤害!”);
printf(“\n您对%s!造成了%d处损坏!”,损坏,entityName);
}
否则{
printf(“\n您错过了,现在是%s攻击…”,entityName);
int entityd20roll=(rand()%20)+1;
printf(“\n%s通过了你的护甲等级,现在将对其造成伤害”);
如果(entityd20roll>userAC){
int edmg=(rand()%12);
int hp=userHp-edmg;
如果(hp<0)
hp*=-1;
printf(“\n获取的点数:\n”);
printf(“%s已处理%d个损坏”,entityName,edmg);
}
否则{
printf(“%s已错过您”,entityName);
}
}
返回0;
}

另外,我如何创建一个内存文件,以便用户不必反复键入所有内容?

我建议包括该库,并尝试使用
cout
执行相同的操作,因为这些命令的工作方式略有不同

例如,
printf(“输入您的装甲类:\n”)
变成
cout我建议(正如@samu_242已经做的那样)使用
std::cout
std::cin
命令进行输入

在您的代码中,
printf

    printf("\n%s passed your armor class, now it will roll for damage");
需要一个字符串(%s),但您没有传递任何内容。此外,您可能考虑使用字符数组,但您只为一个字符分配了内存:

char entityName, dmgType;
这样,
entityName
将只获得用户在输入中给出的第一个字符(例如,如果他/她键入“Goblin”
entityName
将只包含“g”)。要声明字符数组,请使用

char entityName[N];

其中N是数组的最大长度。也有动态分配内存的方法,但我的建议是使用std::string。

返回0是下一个,这将是一个简短的游戏。重定向文件中的输入。请参阅