如何解决此错误?

问题描述 投票:0回答:1

我只是出于娱乐目的而正在申请玩《龙与地下城》,但遇到了问题。我写了多个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;
}

此外,我想知道如何创建一个内存文件,这样用户不必一遍又一遍地键入所有内容。

c++ console-application
1个回答
0
投票

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

例如printf("Type in your armor class: \n");成为cout<<"Type in your armor class: "<<endl;

[scanf("%d", &userAC);成为cin>>userAC;] >>

对于您的文件保存系统,我建议您遵循文件I / O上的课程,例如此视频:https://www.youtube.com/watch?v=Iho2EdJgusQ。然后,您可以将用户的所有选项写入文件,然后在程序启动时读取信息。这样,将保留用户的首选项。

© www.soinside.com 2019 - 2024. All rights reserved.