Home > Net >  getting ╠╠╠╠╠╠╠╠ as an output when printing a string
getting ╠╠╠╠╠╠╠╠ as an output when printing a string

Time:12-22

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]);
    }
}

CodePudding user response:

  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.

  •  Tags:  
  • c
  • Related