The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
C blackjack program help
Discuss C blackjack program help in the C Programming forum on Dev Shed. C blackjack program help C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 29th, 2012, 02:51 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 3
Time spent in forums: 1 h 40 m 41 sec
Reputation Power: 0
|
|
|
C blackjack program help
Hey all, I'm an absolute beginner who just finished reading a C programming book. I'm now moving on to "c programming: a modern approach (2nd edition).
However, before I move on, I'm trying to get a program running that was supplied in the back of the book. It's a blackjack program, and I copied it word for word to try and better understand the concepts and see the full program in action.
Here are the three errors I keep getting from the codeblocks compiler:
In function 'dispTitle':
1)error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
2)error: old-style parameter declarations in prototyped function definition
3)error: expected '{' at end of input
I tried my best to come up with a solution. Can anyone help me out? Here is the code below. thanks so much!
c Code:
Original
- c Code |
|
|
|
/* filename: BLACKJACK.C This program plays a game of Blackjack with you. The computer is the dealer and you are the victim-er, I mean player. The dealer gets a card that you can see. The dealer then asks if you want another card by asking "Hit" or "Stand". If you choose to hit, the dealer draws or stands, and the game is played out according to the cards you and the dealer have. As the real Blackjack, the dealer stands on 17. The winner is announced only after both the player's and the dealer's hands are finished. */ /* **************************************** ANSI C standard header giles appear next */ #include <stdio.h> #include <stdlib.h> #include <time.h> #include <ctype.h> #define BELL '\a' #define DEALER 0 #define PLAYER 1 /* Must keep two sets of totals for dealer and for player. The first set counts Aces as 1 and the second counts Aces as 11. Unlike real world blackjack, this program doesnt allow aces to be 1 while others aces are 11 in the same hand */ #define ACELOW 0 #define ACEHIGH 1 /* Only one global variable is used in this entire program. The variable holds 0, which means false initially. Once the user enters his or her name in initCardsScreen(), this variable is set to 1 (for true), so the name is never asked for again the rest of the program. */ int AskedForName = 0; /* false initialy */ /* ******************************************************** This program's specific prototypes */ void dispTitle(void) void initCardsScreen(int cards [52], int playerPoints[2], int dealerPoint[2], int total [2], int * numCards); int dealCard(int * numCards, int cards[52]); void dispCard(int cardDrawn, int points [2]); void totalIt(int points [2], int total [2], int who); void dealerGetsCard(int *numCards, int cards [52], int dealerPoints[2]); void PlayerGetsCard(int *numCards, int cards [52], int playerPoints [2]); char getsAns(char mesg[]); void findWinner(int total[2]); /* ********************************************************* here is main() */ int main() { int numCards; /*equals 52 at beginning of each game */ int cards [52], playerPoints[2], dealerPoints[2], total[2]; char ans /* for users hit/stand or yes/no response */ do { initCardsScreen(cards, playerPoints, dealerPoints, total, &numCards); dealerGetsCard(&numCards, cards, dealerPoints); printf("\n"); /* prints a blank line */ PlayerGetsCard(&numCards, cards, playerPoints); PlayerGetsCard(&numCards, cards, playerPoints); do { ans = getAns("Hit or stand (H/S)? "); if (ans == 'H') { PlayerGetsCard(&numCards, cards, playerPoints); } } while (ans != 'S'); totalIt(playerPoints, total, PLAYER); /* players total */ do{ dealerGetsCard(&numCards, cards, dealerPoints); } while (dealerPoints[ACEHIGH] < 17); /* 17: dealer stops */ totalIt(dealerPoints, total, DEALER); /* Dealer's total */ findWinner(total); ans = getAns ("\nPlay Again (Y/N)? "); } while (ans == 'Y'); return; /* *************************************************** this function initializes the fave value of the deck of cards by putting four sets of 1-13 in the 52-card array. also clears the screen and displays a title. */ void initCardsScreen(int cards[52], int playerPoints[2], int dealerPoints[2], int total[2], int *numCards) { int sub, val = 1; /* this function's work variables */ char firstName [15]; /*Holds user's first name */ *numcards = 52; /* holds running total of number of cards */ for (sub = 0; sub <= 51; sub++) { /*counts from 0 to 51 */ val = (val == 14) ? 1 : val; /*if val is 14, resest to 1 */ cards[sub] = val; val++; } for (sub = 0; sub <= 1; sub++) /*counts from 0 to 1 */ { playerPoints[sub]=dealerPoints[sub]=total[sub]=0; } dispTitle(); if (AskedForName == 0) /*name asked for only once */ { printf("Ok, %s, get ready for casino action!\n\n", firstName ); getchar(); /* discards newline. you can safely */ /* ignore compiler warning here. */ } return; } /* *********************************************************************** this function gets a card for the player and updates the player's points. */ void PlayerGetsCard(int *numCards, int cards[52], int playerPoints[2]) { int newCard; newCard = dealCard(numCards, cards); dispCard(newCard, playerPoints); } /* ********************************************************************** this function gets a card for the dealer and updates the dealer's points. */ void dealerGetsCard(int *numCards, int cards[52], int dealerPoints[2]) { int newCard; newCard = dealCard(numCards, cards); dispCard(newCard, dealerPoints); } /* ****************************************************************** this function gets a card from the deck and stores it in either the dealer's or the player's hold of cards. */ int dealCard(int * numCards, int cards[52]) { int cardDrawn, subDraw; time_t t; /* gets random-nnumber generator */ srand(time(&t)); /* seeds random-nnumber generator */ subDraw = (rand() % (*numCards)); cardDrawn = cards[subDraw]; cards[subDraw] = cards[*numCards -1]; (*numCards)--; return cardDrawn; } /* ***************************************************************** displays the last drawn card and updates points with it. */ void dispCard(int cardDrawn, int points[2]) { switch (cardDrawn) { case(11) : printf("%s\n", "Jack"); points[ACELOW] += 10; points[ACEHIGH] += 10; break; case(12) : printf("%s\n", "Queen"); points[ACELOW] += 10; points[ACEHIGH] +=10; break; case(13) : printf("%s\n", "King"); points[ACELOW] += 10; points[ACEHIGH] += 10; break; default : points[ACELOW] += cardDrawn; if(cardDrawn == 1) { points[ACEHIGH] += 11; } else { points[ACEHIGH] += cardDrawn; } return; } /* *********************************************************** tigures the total for p[layer or dealer to see who won. this function takes into account the fact that Ace is either 1 or 11. */ void totalIt(int points[2], int total[2], int who) { /* the following routine first looks to seee if the total points counting aces as 1 is equal to the total points counting aces as 11. if so, or if the total points counting aces as 11 is more than 21, the program uses the total with aces as 1 only. */ if ((points[ACELOW] == points[ACEHIGH]) || (points [ACESHIGH] > 21)) { total[who] = points[ACELOW]; } /* keep all aces as 1 */ else { total[who] = points[ACEHIGH]; } /* keeps all aces at it */ if (who == PLAYER) /* determines the message printed */ { printf("You have a total of %d\n\n", total [PLAYER ]); } else { printf("The house stands with a total of %d\n\n", total [DEALER ]); } return; } /* ***************************************************************** prints the winning player. */ void findWinner(int total[2]) { if (total[DEALER] == 21) { printf("The house wins.\n"); return;} if ((total[DEALER] > 21) && (total[PLAYER] > 21)) { printf("%s", "Nobody wins.\n"); return; } if ((total[DEALER]>=total[PLAYER]) && (total[DEALER]<21)) { return; } if ((total[PLAYER] > 21) && (total[DEALER] < 21)) { return; } printf("%s%c", "You win!\n", BELL ); return; } /* ****************************************************************** gets the user's uppercase, single-cahracter response. */ char getAns(char mesg[]) { char ans; printf("%s", mesg ); /*p rints the prompt message passed */ ans = getchar(); getchar(); /* discards newline. you can safely */ /* ignore compiler warning here. */ return toupper(ans); } /* ********************************************************************** Clears everything off the screen. */ void dispTitle(void) { int i = 0; while (i < 25) { i++; } printf("\n\n*Step right up to the Blackjack tables*\n\n"); return; } }
Last edited by shood1234 : September 29th, 2012 at 02:56 PM.
Reason: syntax hi-lighter
|

September 29th, 2012, 03:11 PM
|
|
|
|
You're missing a semicolon at 39;
You're missing a semicolon at line 60;
I think you mean *numCards at line 97 (mind the CAPS!);
The macro is ACEHIGH at line 195 (not ACESHIGH).
Happy Coding!
|

September 29th, 2012, 03:22 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Code:
/* ********************************************************
This program's specific prototypes */
void dispTitle(void)
void initCardsScreen(int cards [52], int playerPoints[2],
int dealerPoint[2], int total [2],
int * numCards);
Please note that there is no semicolon at the end if the prototype for dispTitle. There needs to be.
Most compilers try to be smart and to continue trying to compile the code after having encountered an error. Often this will work, but sometimes the error is such that the compiler has an entirely wrong idea of where it is in your program, which results in a large number of errors that won't make any sense to you.
That missing semicolon appears to be an error that can cause such a chain reaction, such that the compiler might have thought that it's compiling the body of the dispTitle function. When you correct that error, a lot, if not all, of those other errors should go away.
|

September 29th, 2012, 03:42 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 3
Time spent in forums: 1 h 40 m 41 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by bdb You're missing a semicolon at 39;
You're missing a semicolon at line 60;
I think you mean *numCards at line 97 (mind the CAPS!);
The macro is ACEHIGH at line 195 (not ACESHIGH).
Happy Coding! |
Thanks so much for the help bdb! I fixed the errors you mentioned. However, it is now giving me about 16 errors, saying "undefined reference to:" All of the variables such as dispTitle & initCardsScreen are being called out as this error. Any ideas?
Thanks again!
|

September 29th, 2012, 03:58 PM
|
|
|
Quote: | Originally Posted by shood1234 ... it is now giving me about 16 errors, saying "undefined reference to:" ... |
C is a case-sensitive language.
Make sure all your prototypes, function calls, and function definitions match! They must match in case as well as name.
I notice you have a prototype for getsAns() ... but you call and define getAns(). At the time of the call there is no defintion available.
|

September 29th, 2012, 09:06 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 3
Time spent in forums: 1 h 40 m 41 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by bdb C is a case-sensitive language.
Make sure all your prototypes, function calls, and function definitions match! They must match in case as well as name.
I notice you have a prototype for getsAns() ... but you call and define getAns(). At the time of the call there is no defintion available. |
Well, I've gone through the code over and over, and it runs!
but...it runs with strange problems. The dealer is handing out random number such as (12312451231) instead of 1-12. doing some strange things..
I'm certainly learning and I appreciate everyones help!
Thanks
newest code:
c Code:
Original
- c Code |
|
|
|
/* filename: BLACKJACK.C This program plays a game of Blackjack with you. The computer is the dealer and you are the victim-er, I mean player. The dealer gets a card that you can see. The dealer then asks if you want another card by asking "Hit" or "Stand". If you choose to hit, the dealer draws or stands, and the game is played out according to the cards you and the dealer have. As the real Blackjack, the dealer stands on 17. The winner is announced only after both the player's and the dealer's hands are finished. */ /* **************************************** ANSI C standard header giles appear next */ #include <stdio.h> #include <stdlib.h> #include <time.h> #include <ctype.h> #define BELL '\a' #define DEALER 0 #define PLAYER 1 /* Must keep two sets of totals for dealer and for player. The first set counts Aces as 1 and the second counts Aces as 11. Unlike real world blackjack, this program doesnt allow aces to be 1 while others aces are 11 in the same hand */ #define ACELOW 0 #define ACEHIGH 1 /* Only one global variable is used in this entire program. The variable holds 0, which means false initially. Once the user enters his or her name in initCardsScreen(), this variable is set to 1 (for true), so the name is never asked for again the rest of the program. */ int AskedForName = 0; /* false initialy */ /* ******************************************************** This program's specific prototypes */ void dispTitle(void); void initCardsScreen(int cards[52], int playerPoints[2], int dealerPoint[2], int total[2], int *numCards); int dealCard(int *numCards, int cards[52]); void dispCard(int cardDrawn, int points[2]); void totalIt(int points[2], int total[2], int who); void dealerGetsCard(int *numCards, int cards[52], int dealerPoints[2]); void PlayerGetsCard(int *numCards, int cards[52], int playerPoints[2]); char getAns(char mesg[]); void findWinner(int total[2]); /* ********************************************************* here is main() */ int main() { int numCards; /*equals 52 at beginning of each game */ int cards[52], playerPoints[2], dealerPoints[2], total[2]; char ans; /* for users hit/stand or yes/no response */ do { initCardsScreen (cards, playerPoints, dealerPoints, total, &numCards); dealerGetsCard(&numCards, cards, dealerPoints); printf("\n"); /* prints a blank line */ PlayerGetsCard(&numCards, cards, playerPoints); PlayerGetsCard(&numCards, cards, playerPoints); do { ans = getAns("Hit or stand (H/S)? "); if (ans == 'H') { PlayerGetsCard(&numCards, cards, playerPoints); } } while (ans != 'S'); totalIt(playerPoints, total, PLAYER); /* players total */ do{ dealerGetsCard(&numCards, cards, dealerPoints); } while (dealerPoints[ACEHIGH] < 17); /* 17: dealer stops */ totalIt(dealerPoints, total, DEALER); /* Dealer's total */ findWinner(total); ans = getAns ("\nPlay Again (Y/N)? "); } while (ans == 'Y'); return; } /* *************************************************** this function initializes the fave value of the deck of cards by putting four sets of 1-13 in the 52-card array. also clears the screen and displays a title. */ void initCardsScreen(int cards[52], int playerPoints[2], int dealerPoints[2], int total[2], int *numCards) { int sub, val = 1; /* this function's work variables */ char firstName [15]; /*Holds user's first name */ *numCards = 52; /* holds running total of number of cards */ for (sub = 0; sub <= 51; sub++) { /*counts from 0 to 51 */ val = (val == 14) ? 1 : val; /*if val is 14, resest to 1 */ cards[sub] = val; val++; } for (sub = 0; sub <= 1; sub++) /*counts from 0 to 1 */ { playerPoints[sub]=dealerPoints[sub]=total[sub]=0; } void dispTitle(); if (AskedForName == 0) /*name asked for only once */ { printf("Ok, %s, get ready for casino action!\n\n", firstName ); getchar(); /* discards newline. you can safely */ /* ignore compiler warning here. */ } return; } /* *********************************************************************** this function gets a card for the player and updates the player's points. */ void PlayerGetsCard(int *numCards, int cards[52], int playerPoints[2]) { int newCard; newCard = dealCard(numCards, cards); dispCard(newCard, playerPoints); } /* ********************************************************************** this function gets a card for the dealer and updates the dealer's points. */ void dealerGetsCard(int *numCards, int cards[52], int dealerPoints[2]) { int newCard; newCard = dealCard(numCards, cards); dispCard(newCard, dealerPoints); } /* ****************************************************************** this function gets a card from the deck and stores it in either the dealer's or the player's hold of cards. */ int dealCard(int * numCards, int cards[52]) { int cardDrawn, subDraw; time_t t; /* gets random-nnumber generator */ srand(time(&t)); /* seeds random-nnumber generator */ subDraw = (rand() % (*numCards)); cardDrawn = cards[subDraw]; cards[subDraw] = cards[*numCards -1]; (*numCards)--; return cardDrawn; } /* ***************************************************************** displays the last drawn card and updates points with it. */ void dispCard(int cardDrawn, int points[2]) { switch (cardDrawn) { case(11) : printf("%s\n", "Jack"); points[ACELOW] += 10; points[ACEHIGH] += 10; break; case(12) : printf("%s\n", "Queen"); points[ACELOW] += 10; points[ACEHIGH] +=10; break; case(13) : printf("%s\n", "King"); points[ACELOW] += 10; points[ACEHIGH] += 10; break; default : points[ACELOW] += cardDrawn; if(cardDrawn == 1) { points[ACEHIGH] += 11; } else { points[ACEHIGH] += cardDrawn; } return; } /* *********************************************************** tigures the total for p[layer or dealer to see who won. this function takes into account the fact that Ace is either 1 or 11. */ void totalIt(int points[2], int total[2], int who) { /* the following routine first looks to seee if the total points counting aces as 1 is equal to the total points counting aces as 11. if so, or if the total points counting aces as 11 is more than 21, the program uses the total with aces as 1 only. */ if ((points[ACELOW] == points[ACEHIGH]) || (points [ACEHIGH] > 21)) { total[who] = points[ACELOW]; } /* keep all aces as 1 */ else { total[who] = points[ACEHIGH]; } /* keeps all aces at it */ if (who == PLAYER) /* determines the message printed */ { printf("You have a total of %d\n\n", total [PLAYER ]); } else { printf("The house stands with a total of %d\n\n", total [DEALER ]); } return; } /* ***************************************************************** prints the winning player. */ void findWinner(int total[2]) { if (total[DEALER] == 21) { printf("The house wins.\n"); return;} if ((total[DEALER] > 21) && (total[PLAYER] > 21)) { printf("%s", "Nobody wins.\n"); return; } if ((total[DEALER]>=total[PLAYER]) && (total[DEALER]<21)) { return; } if ((total[PLAYER] > 21) && (total[DEALER] < 21)) { return; } printf("%s%c", "You win!\n", BELL ); return; } /* ****************************************************************** gets the user's uppercase, single-cahracter response. */ char getAns(char mesg[]) { char ans; printf("%s", mesg ); /*p rints the prompt message passed */ ans = getchar(); getchar(); /* discards newline. you can safely */ /* ignore compiler warning here. */ return toupper(ans); } /* ********************************************************************** Clears everything off the screen. */ void dispTitle(void) { int i = 0; while (i < 25) { i++; } printf("\n\n*Step right up to the Blackjack tables*\n\n"); return; }
|

October 2nd, 2012, 07:19 PM
|
 |
Contributing User
|
|
|
|
|
Line #180
Pretty sure
printf("%d\n, cardDrawn");
is wrong.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|