Jake Delso
Jake Delso

Reputation: 1

Having trouble getting the stat allocation function of my RPG to work

When I run my code and get to the stat allocation function, intel already has a point allocated to it while the rest of the stats don't. Why is variable intel equal to 1 before any points have been allocated to it? I would have thought initializing it in the beginning as 0 with all the other variables would have it set at 0 by the time the player is called to begin allocating points? How is it that this is true for all the other variables aside from intel too?

#include <stdio.h>

void startgame();
int makechar(int ref, int dex, int intel, int str);

int main(){
    int health = 100;
    int ref, dex, intel, str = 0;

    startgame();
    makechar(ref, dex, intel, str);
    return 0;
}

void startgame(){
    char decision[10];

    do{
        printf("Welcome to Grounded Python, a game by Delso Interactive. Type 'start' to continue!\n");
        fgets(decision, 10, stdin);
        decision[strcspn(decision, "n")] = '\0';

        if(strcspn(decision, "start") == 0){
            break;
        }
        else if (strcspn(decision, "quit") == 0){
            printf("quiting...");
            exit(0);
        }
    }

    while(strcspn(decision, "start") != 0);
    return 1; 
}

int makechar(int ref, int dex, int intel, int str){
    int pointpool = 10;
    int sel = 0;
    
    printf("[BOOTING DEFIB.INC BIOS]\n");
    printf("[ERROR FAILED TO LOAD USER STATISTICS.]\n[REQUESTING USER ENTER THEM MANUALLY.]\n[FAILURE TO DO SO WITHIN 7 BUISNESS DAYS WILL RESULT IN TERMINATION OF EMPLOYMENT.]\n[PLEASE CONTACT IT SUPPORT IF YOU HAVE ANY ISSUES COMPLETING THIS TASK.]");
    
    while(pointpool >= 0){
        printf("Allocate statistic points by inputing the integer that corresponds to the statistic you want to increase. You may only increase your statistics by 10 points:\n");
        printf("[1] Reflex = %d\n", ref);
        printf("[2] Dexterity = %d\n", dex);
        printf("[3] Intelligence = %d\n", intel);
        printf("[4] Strength is %d\n", str);
        
        scanf("%d", &sel);

        switch(sel){
            case 1:
                ref = ref + 1;
                pointpool = pointpool - 1;
                break;
            case 2:
                dex = dex + 1;
                pointpool = pointpool - 1;
                break;
            case 3:
                intel = intel + 1;
                pointpool = pointpool - 1;
                break;
            case 4:
                str = str + 1;
                pointpool = pointpool - 1;
                break;
        }
    }
}

I'm expecting the intel stat to be at zero when the user is called to begin inputting stats

Upvotes: 0

Views: 52

Answers (1)

wohlstad
wohlstad

Reputation: 29466

In this line:

int ref, dex, intel, str = 0;

Only str is initialized to 0. The rest are uninitialized.
I.e. it is equivalent to:

int ref;
int dex;
int intel
int str = 0;

Also int makechar(int ref, int dex, int intel, int str) accepts all the parameters by value. This means a copy is passed when you call the function, and modification inside it does not affect the caller.
If you want to modify the variables in main you should pass pointers to them instead.
Another issue is that since the function does not return any value it should be changed to return void.

The function prototype should change to:

void makechar(int * ref, int * dex, int * intel, int * str)

Using the variables in the function should dereference them: *ref, *dex etc.

And calling the function should change to pass their addresses:

makechar(&ref, &dex, &intel, &str);

A final note:
startgame() is declared as void, but contains a return 1; statement. It should probably be removed (as I see that when you call the functions in main you do not expect them to return anything).
On the other hand if you need the functions to return some value, you should probably use this value in main (e.g. by assigning it to a variable, or inspecting it otherwise).

Upvotes: 3

Related Questions