2

i am trying to store a random value (one of these {"1","2","3","4","5","6","7","8","9","+","STOP","<->","COLOR","TAKI"}) inside a string but when i print the string i always get this output.

╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠

here is my code:

typedef struct RandomCard {

char CardsColor;
char CardType[5];

}Card_info;

typedef struct Player {

char Name[20];
int Cards_Amount;
Card_info Deck[50];

}Player_info;


void GetRandomCard(Player_info players, int Index, int StartCards, int RandomNum);

void main()
{
    Player_info players[10];
    int Num_Of_Players;
    char PFirstName[20];
    int Random_Num;


    for (int i = 0; i < Num_Of_Players; i++)
    {
        for (int j = 0; j < 4; j++)
        {
          Random_Num = (rand() % 10);
          GetRandomCard(players[i], j, 0, Random_Num);
          printf("%s", players[i].Deck[j].CardType);
        }
    }
 }



void GetRandomCard(Player_info players,int CardIndex, int StartCards, int RandomNum)
{
    char Color[4] = { 'R', 'G', 'Y', 'B' };
    char CardType[16][5] = {"1","2","3","4","5","6","7","8","9","+","STOP","<->","COLOR","TAKI"};

    if (StartCards == 0)
    {
    strcpy(players.Deck[CardIndex].CardType, CardType[RandomNum]);
    }
}
  • 6
    `╠` means 0xCC or uninitialized stack memory in msvc. Related: [https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations](https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations) – drescherjm Dec 21 '21 at 14:24
  • 5
    `int Num_Of_Players;` this variable is never assigned a value, therefore its content is undetermined when you get to the for loop. – Jabberwocky Dec 21 '21 at 14:27
  • 3
    C passes parameters by value, so you copy `players[i]` in your call of `GetRandomCard()`, which just changes the copy, not the original. – the busybee Dec 21 '21 at 14:27
  • 4
    The string `"COLOR"` is one character too long for `char CardType[16][5]` because there is a trailing NUL (`'\0'`) character as string termination. I suggest to compile the program with GCC using options `-g -fsanitize=address,undefined -Wall -Wextra` to detect memory access errors at runtime. – Bodo Dec 21 '21 at 14:37

1 Answers1

3
  1. int Num_Of_Players; was never initialised. Change that to int Num_Of_Players = 10.

  2. In function GetRandomCard, nothing happens. It changes some of it's local variables but nothing happens. When you pass players[i] into the function, a new copy is made and the function changes that copy, not players[i]. To fix that, use a pointer. Like this:

void GetRandomCard(Player_info *players,int CardIndex, int StartCards, int RandomNum)
{
    char Color[4] = { 'R', 'G', 'Y', 'B' };
    char CardType[16][5] = {"1","2","3","4","5","6","7","8","9","+","STOP","<->","COLOR","TAKI"};

    if (StartCards == 0)
    {
    strcpy(players->Deck[CardIndex].CardType, CardType[RandomNum]);
    }
}
  1. In the same function GetRandomCard, there's one more problem in the line void GetRandomCard(Player_info *players,int CardIndex, int StartCards, int RandomNum) { char Color[4] = { 'R', 'G', 'Y', 'B' }; char CardType[16][5] = {"1","2","3","4","5","6","7","8","9","+","STOP","<->","COLOR","TAKI"};. Look at the second last element, "COLOR". It's of 5 characters plus the null terminated character, means 6. So, it should be char CardType[16][6]. The term "null-terminating character" is pretty self-explanatory. It's a character with the value of 0(not '0') which marks the end of the string. This one is kind of hard to see. I too didn't see it until @Bodo commented that.

Your final corrected code is:

typedef struct RandomCard {

char CardsColor;
char CardType[5];

}Card_info;

typedef struct Player {

char Name[20];
int Cards_Amount;
Card_info Deck[50];

}Player_info;


void GetRandomCard(Player_info players, int Index, int StartCards, int RandomNum);

void main()
{
    Player_info players[10];
    int Num_Of_Players = 10; //There are better ways. Eg. writing sizeof(players) / sizeof(Player_info) instead of 10.
    char PFirstName[20];
    int Random_Num;


    for (int i = 0; i < Num_Of_Players; i++)
    {
        for (int j = 0; j < 4; j++)
        {
          Random_Num = (rand() % 10);
          GetRandomCard(&players[i], j, 0, Random_Num);
          printf("%s", players[i].Deck[j].CardType);
        }
    }
 }



void GetRandomCard(Player_info &players,int CardIndex, int StartCards, int RandomNum)
{
    char Color[4] = { 'R', 'G', 'Y', 'B' };
    char CardType[16][6] = {"1","2","3","4","5","6","7","8","9","+","STOP","<->","COLOR","TAKI"};

    if (StartCards == 0)
    {
    strcpy(players->Deck[CardIndex].CardType, CardType[RandomNum]);
    }
}

And, your code, naming standard, etc are so bad that it decreases much of the readability. The function GetRandomCard is the most horribly named thing ever. It doesn't actually fetch a random card, instead writing it to the player. Rename that to something more intuitive. Use snake_case if possible or camelCase is not. I can't guarantee this will work though.

Shambhav
  • 813
  • 7
  • 20