I'm a complete beginner working through the "C Programming for Absolute beginners" book.
Every once in a while I run into an example that won't compile correctly. I'm trying to run an end of chapter program the book has written in it. And it compiles, and tries to run, then crashes.
Being so new to c programming I'm not entirely sure what to look for since the Dev-c++ compiler I use didn't pick up an error before running the program.
Below is the code, would much appreciate any thoughts or tips, anything I missed.
Code:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
//define new data type
typedef struct deck{
char type[10];
char used;
int value;
}aDeck; //end type
//func proto
void shuffle( aDeck *);
main()
{//start main
int x,y;
aDeck myDeck[52];
srand ( time(NULL) );
//initialize struct array
for (x=0; x<3; x++){//start for
for(y=0; y<13; y++){//start for
switch (x){//start switch
case 0:
strcpy(myDeck[y].type, "diamonds");
myDeck[y].value=y;
myDeck[y].used='n';
break;
case 1:
strcpy(myDeck[y + 13].type, "clubs");
myDeck[y+13].value=y;
myDeck[y+13].used='n';
break;
case 2:
strcpy(myDeck[y + 26].type, "hearts");
myDeck[y+26].value=y;
myDeck[y+26].used='n';
break;
case 3:
strcpy(myDeck[y +39].type, "spades");
myDeck[y+39].value=y;
myDeck[y+39].used='n';
break;
}//end switch
}//end inner for
}//end outer for
shuffle(myDeck);
}//end main
void shuffle(aDeck * thisDeck)
{//start func
int x;
int iRnd;
int found=0;
system("cls");
printf("\nYour five cards are: \n\n");
while (found <5){//start while
iRand=rand() % 51;
if (thisDeck[iRnd].used=='n'){//start if
switch(thisDeck[iRnd].value){//start switch
case 12:
printf("Ace of %s\n", thisDeck[iRnd].type);
break;
case 11:
printf("King of %s\n", thisDeck[iRnd].type);
break;
case 10:
printf("Queen of %s\n", thisDeck[iRnd].type);
break;
case 9:
printf("Jack of %s\n", thisDeck[iRnd].type);
break;
default:
printf("%d of ", thisDeck[iRnd].value+2);
printf("%s\n", thisDeck[iRnd].type);
break;
}//end switch
thisDeck[iRnd].used='y';
found = found+1;
}//end if
}//end while
}//end shuffle