when I am trying to deal 52 card to four players using structure, The code works but the problem is in output it got some mistake:
#include <stdio.h>
#include <time.h>
typedef struct {
char *value;
char *type;
}card;
int main()
{
srand(time(NULL));
int i, r;
char name[4][9] = {{"diamonds"},{"clubs"},{"hearts"},{"spades"}};
char num[13][6] = {{"ace"},{"two"},{"three"},{"four"},{"five"},{"six"},{"seven"},{"eight"},{"nine"},{"ten"},{"jack"},{"queen"},{"king"}};
card c[52];
card temp;
card player1[13], player2[13], player3[13], player4[13];
for (i=0 ; i<52 ; i++){
c[i].type = name[i % 4];
c[i].value = num[i % 13];
}
for (i=0 ; i<52 ; i++){
r= rand() % 52;
temp = c[i];
c[i] = c[r];
c[r] = temp;
}
for (i=0 ; i<52 ; i++){
switch(i+1 % 4){
case 0:
player1[i].type= c[i].type;
player1[i].value= c[i].value;
break;
case 1:
player2[i].type= c[i].type;
player2[i].value= c[i].value;
break;
case 2:
player3[i].type= c[i].type;
player3[i].value= c[i].value;
break;
default:
player4[i].type= c[i].type;
player4[i].value= c[i].value;
}
}
printf("Player 1 \n");
for (i=0 ; i<13 ; i++){
printf("%s %s\n" , player1[i].type, player1[i].value);
}
printf("\n");
printf("Player 2 \n");
for (i=0 ; i<13 ; i++){
printf("%s %s\n" , player2[i].type, player2[i].value);
}
printf("\n");
printf("Player 3 \n");
for (i=0 ; i<13 ; i++){
printf("%s %s\n" , player3[i].type, player3[i].value);
}
printf("\n");
printf("Player 4 \n");
for (i=0 ; i<13 ; i++){
printf("%s %s\n" , player4[i].type, player4[i].value);
}
printf("\n");
return 0;
}
output: Player 1 hearts nine diamonds king hearts ten diamonds ten spades jack clubs king hearts four spades eight spades king clubs jack spades two diamonds jack hearts jack
Player 2 spades six hearts queen diamonds ace spades five diamonds six hearts seven clubs eight clubs seven spades ace hearts three hearts king diamonds two clubs six
Player 3 spades three diamonds seven clubs queen hearts six hearts five diamonds three spades nine clubs three spades queen clubs ace clubs two diamonds four hearts ace
Player 4 à└ëD$@ï╪☼ê°♥ (null) (null) (null) hearts two hearts eight diamonds nine clubs five spades seven clubs nine spades four clubs ten diamonds eight diamonds queen clubs four
It got in player 4 null and strange characters, any solution please.