Home > Software design >  how do i change values of a global struct inside main/another function in c?
how do i change values of a global struct inside main/another function in c?

Time:12-24

so i'm making a blackjack game in c for fun and to practice coding. currently the way i have it set up is instead of just making a variable for the card, since face cards have the same value and aces have two different possible values, i made a struct to store a few different parameters and a function to change said parameters but it currently won't change anything when i pass the card through the function. below is my code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdbool.h>


struct card {

int value;
char displayName;
bool changedName; //I did this to know whether to print as an int or char in the print statements
bool ace; //I did this because aces can be either 1 or 11 and I want to accommodate for that

};

int generateCards() {

int card = (rand() % 13)   1;
return card;

}

//I have also tried making this return the struct card but nothing changes
void cleanUpCards(struct card cardInput) {

if (cardInput.value == 1) {
    cardInput.displayName = 'A';
    cardInput.changedName = true;
    cardInput.ace = true;
}
else if (cardInput.value == 11) {
    cardInput.displayName = 'J';
    cardInput.changedName = true;
}
else if (cardInput.value == 12) {
    cardInput.displayName = 'Q';
    cardInput.changedName = true;
}
else if (cardInput.value == 13) {
    cardInput.displayName = 'K';
    cardInput.changedName = true;
}

if (cardInput.changedName) {
    cardInput.value = 10;
}

}

int main() {

srand(time(NULL)); 


int selection;
int looping = 0;
while (looping != -1) {

    printf("Welcome to Blackjack! Please select an option below:");
    printf("\n==========================================================");
    printf("\n1. Play the game");
    printf("\n2. Exit");
    printf("\n\nYour choice: ");
    scanf("%d", &selection);

    //here is where the actual game starts
    if (selection == 1) {

        //I used a struct here to store both the value of the card and how to display it if it's over 10.
        struct card playerCard1, playerCard2, dealerCard1, dealerCard2;
        playerCard1.value = generateCards();
        playerCard2.value = generateCards();
        dealerCard1.value = generateCards();
        dealerCard2.value = generateCards();

        cleanUpCards(playerCard1);
        cleanUpCards(playerCard2);
        cleanUpCards(dealerCard2);
        cleanUpCards(dealerCard2);

        //This is just to check whether anything above 10 is displayed, and from this I can see that it isn't working... could be an issue with the print statement?
        printf("%d\t%d\n\n%d\t%d\n\n", playerCard1.value, playerCard2.value, dealerCard1.value, dealerCard2.value);

    }
    else if (selection == 2) {
        break;
    }

}

return 0;

}

CodePudding user response:

You need to pass the struct as a pointer, otherwise the function will work on a copy of the struct. Change the signature to

void cleanUpCards(const struct card *cardInput) {
   ...
}

and access the fields of cardInput with "->" instead of ".". Also, call it with

cleanUpCards(&playerCard1);
...
  •  Tags:  
  • c c
  • Related