Marko Vuger
Marko Vuger

Reputation: 13

How do I fix this bug?

I am making an application for playing Dungeons & Dragons, just for fun, and I ran into a problem. I've written multiple printfs and somehow it stopped working. When it finishes the first calculation and when it has to move on to the next, then it stops. I have no idea why.

This is the code:

#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;
}

Also, how can I create a memory file so the user doesn't have to type in everything over and over again?

Upvotes: 0

Views: 61

Answers (2)

Eddymage
Eddymage

Reputation: 1137

I suggest (as @samu_242 already has done) to use the std::cout and std::cin commands for the inputs.

In your code, the printf

    printf("\n%s passed your armor class, now it will roll for damage");

expects a string (%s), but you did not pass nothing. Moreover, maybe you had in mind to use array of chars, but you allocated memory for just one char:

char entityName, dmgType;

In this way, entityName will get only the first char that the user gives in input (e.g., if he/she types "Goblin" entityName will have only "G"). To declare an array of char, use

char entityName[N];

where N is the maximum length of the array. There are also ways to dynamically allocate memory, but my advise is to use the std::string.

Upvotes: 0

samu_242
samu_242

Reputation: 113

I suggest including the library and trying the same thing with cout<< and cin>> because these commands work a little different.

So for example printf("Type in your armor class: \n"); becomes cout<<"Type in your armor class: "<<endl;

And scanf("%d", &userAC); becomes cin>>userAC;

And for your file saving system, I suggest you follow a class on file I/O like for example this video: https://www.youtube.com/watch?v=Iho2EdJgusQ. You can then write all the user's options to a file and then read the information when the programs starts. This way the user's preferences will be kept.

Upvotes: 1

Related Questions