[C] Finished Lab3
Difference between #define and global variables
#define MAX 9
Structure and typedof
struct card{ };
typedof struct card Card;
Only local variables (not allowed to use global variables)
Check valid input
int count;
if(count = scanf("%d", &pnPlayers)!=1) { };
Check valid input with while loop (in case of input is string type)
int count
while((count=fgetc(stdin))!='\n'){ };
** fgets is better instead of fgetc
Using ctrncpy()
#define MAX_CARDS 52
int i = 0;
for(i=0; i<MAX_CARDS; i++) {
deck[i].rank = ranks[i%MAX_RANKS];
strncpy(deck[i].suit, suits[i/MAX_RANKS], MAX);
https://www.tutorialspoint.com/c_standard_library/c_function_strncpy.htm
Swap with rand()
for(i=0; i<MAX_CARDS; i++) {
swapper = rand() % MAX_CARDS;
temp = deck[i];
deck[i] = deck[swapper];
deck[swapper] = temp;
Comments
Post a Comment