# blackjack program

• 09-19-2007
mackieinva
blackjack program
hi all, next im working on a blackjack program. cards are just 2-10, giving the user 2 cards and asking 1 for another card or 2 for no.

if they say no, hit 21, or go over they are asked if they want to play again, this is where im struggling. it doesnt start over and read two new cards like i thought it would, it just takes the same two cards. sorry about the indentation and format im working on it.

Code:

```#include <stdio.h> #include <stdlib.h> #include <time.h> int main() {         int i;     int total;     int cardchoice, gamechoice;     int card1, card2, newcard;         srand(time(NULL));         for(i = 0; i < 2; i++) {           card1 = rand() % 10 + 1;             if (card1 == 1) {                       card1 = rand() % 10 + 1;                             }           card2 = rand() % 10 + 1;             if (card2 == 1) {                       card2 = rand()% 10 + 1;}     }         printf("You have the following cards: %d, %d\n", card1, card2);       total = card1 + card2;       printf("Your total is currently %d\n",total);         for (i = 0; i < 100000000; i++) {     printf("Would you like another card? Enter 1 for y or 2 for n: ");     scanf("%d",&cardchoice);             if (cardchoice == 1) {                   newcard = rand() % 10 + 1;               if (newcard == 1) {                           newcard = rand()% 10 + 1; }           total = total + newcard;                                         printf("\nYou got a %d\n", newcard);           printf("Your total is: %d\n",total);                     if (total > 21) {                     printf("You lose, you went over 21.\n");                     printf("Would you like to play again? Enter 1 for y or 2 for n: ");                     scanf("%d", &gamechoice);                     if (gamechoice == 1) {                                       for(i = 0; i < 2; i++) {                                               card1 = rand() % 10 + 1;                                               card2 = rand() % 10 + 1;                                                           }                                        }                     if (gamechoice == 2) {                                   break;                                   }           }           else if (total == 21) {                     printf("Congratulations!! You win. You got blackjack!!\n");                     printf("Would you like to play again? Enter 1 for y or 2 for n: ");                     scanf("%d", &gamechoice);                                         if (gamechoice == 1) {                                                   for(i = 0; i < 2; i++) {                                                         card1 = rand() % 10 + 1;                                                         card2 = rand() % 10 + 1;                                                   }                                           }                                         if (gamechoice == 2) {                                                     break;                                         }                                                               }         }           if (cardchoice == 2) {                         printf("Would you like to play again? Enter 1 for y or 2 for n: ");                         scanf("%d", &gamechoice);                                             if (gamechoice == 1) {                                                 for(i = 0; i < 2; i++) {                                                         card1 = rand() % 10 + 1;                                                         card2 = rand() % 10 + 1;                                                 }                                             }                                   if (gamechoice == 2) {                                                   break;                                   }           }        }    system("PAUSE"); return 0; }```
• 09-19-2007
mike_g
It looks like you are dealing the player starting hand outside of you main loop. I think you might need to put it inside for it to deal new cards each game.

also here:
Code:

`for (i = 0; i < 100000000; i++)`
You could jsut do:
Code:

`for(;;)`
To loop infinitely.
• 09-19-2007
mackieinva
yeah i tried to put that into the loop but then it puts another set of cards after you get the total from new cards.

so if i have 2 and 10 for 12 and ask for another card im returned

you got a 5
youre total is 8
do you want another card?

id like to make it so the first cards you get are outside of the loop and taken in, and when those are done with (you hit 21, go over, or want to start a new game) the gamechoice of 1 gives you two new cards but it doesnt work quite right. thanks for the help though mike g. if it hits 21 or goes over after the first draw it works well
• 09-19-2007
i would put everything you have in a do-while loop, except for the first few statements (the variable declarations and srand).

at the beginning of the do, you determine the 2 cards to be 'dealt'. then have another loop (another do-while loop) looping over the choice of hitting or staying, and exiting the inner do loop once they want to stay. you then have the 'computer' play (if you are including that, its quite easy). finally you print out the results of the game, and ask to play again or not. this is what controls your main/outer do-while loop: if they want to play again, it starts over, otherwise exit the loop and the program finishes.

once you have it better structured you can follow it easier, hopefully, and find out where your going wrong.
• 09-19-2007
mackieinva
ive never done a do while loop before, do you mind showing me what you mean?
• 09-19-2007
mackieinva
got it to work!!!!

i just made a goto label, didnt even think of that. thanks for the help again yall i really appreciate it like always

Code:

```#include <stdio.h> #include <stdlib.h> #include <time.h> int main() {         int i;     int total;     int cardchoice, gamechoice;     int card1, card2, newcard;         srand(time(NULL));         start:           for(i = 0; i < 2; i++) {           card1 = rand() &#37; 10 + 1;             if (card1 == 1) {                       card1 = rand() % 10 + 1;                             }           card2 = rand() % 10 + 1;             if (card2 == 1) {                       card2 = rand()% 10 + 1;}     }         printf("\n\nYou have the following cards: %d, %d\n", card1, card2);         total = card1 + card2;       printf("Your total is currently %d\n",total);                             for (i = 0; i < 100000000; i++) {     printf("Would you like another card? Enter 1 for y or 2 for n: ");     scanf("%d",&cardchoice);             if (cardchoice == 1) {                   newcard = rand() % 10 + 1;               if (newcard == 1) {                           newcard = rand()% 10 + 1; }           total = total + newcard;                                         printf("\nYou got a %d\n", newcard);           printf("Your total is: %d\n",total);                     if (total > 21) {                     printf("You lose, you went over 21.\n");                     printf("Would you like to play again? Enter 1 for y or 2 for n: ");                     scanf("%d", &gamechoice);                     if (gamechoice == 1) {                                       goto start;                                       }                     if (gamechoice == 2) {                                   break;                                   }           }           else if (total == 21) {                     printf("Congratulations!! You win. You got blackjack!!\n");                     printf("Would you like to play again? Enter 1 for y or 2 for n: ");                     scanf("%d", &gamechoice);                                         if (gamechoice == 1) {                                                   goto start;                                           }                                         if (gamechoice == 2) {                                                     break;                                         }                                                               }         }           if (cardchoice == 2) {                         printf("Would you like to play again? Enter 1 for y or 2 for n: ");                         scanf("%d", &gamechoice);                                             if (gamechoice == 1) {                                                 goto start;                                             }                                   if (gamechoice == 2) {                                                   break;                                   }           }        }    system("PAUSE"); return 0; }```
• 09-20-2007
wyliek
You should learn to use while, for and do while loops.
You really, really should learn to indent your code properly. I did not even try looking at your code because it is too ugly and difficult to understand.
Try never to use goto especially in the case where it is a quick fix to a problem you do not understand. Here is a working example with proper indenting that does not use goto:

Code:

```#include <stdio.h> #include <stdlib.h> #include <time.h> int main() {     int total, gamechoice;     int card1, card2, newcard;     int looping=1;           do     {         /* initialize random seed: */         srand ( time(NULL) );         total = 0;         newcard = 0;         card1 = rand() % 10 + 1;          card2 = rand() % 10 + 1;          printf("\nyour cards are: %d and %d\n", card1, card2);         total = card1 + card2;         while (1)         {             printf("your total is: %d\n", total);             printf("would you like another? 1=yes 2=no\n");             scanf("%d", &gamechoice);             if (1 == gamechoice)             {                 newcard = rand() % 10 + 1;                 printf("new card is: %d\n", newcard);                 total += newcard;                 if (total == 21)                 {                     printf("21 you win! play again? 1=yes 2=no\n");                     scanf("%d", &gamechoice);                     if (gamechoice == 2)                     {                         looping = 0;                         break;                                }                     break;                 }                 else if (total > 21)                 {                     printf("you have %d\n", total);                     printf("you lose! Play again? 1=yes 2=no\n");                     scanf("%d", &gamechoice);                     if (gamechoice == 2)                     {                         looping = 0;                         break;                                }                     break;                 }             }             else             {                 printf("play again? 1=yes 2=no\n");                 scanf("%d", &gamechoice);                 if (gamechoice == 2)                 {                     looping = 0;                     break;                            }                 break;             }         }     } while (looping);         system("PAUSE");     return 0; }```
• 09-20-2007
matsp
```#include <stdio.h> #include <stdlib.h> #include <time.h> int main() {     int total, gamechoice;     int card1, card2, newcard;     int looping=1;           do     {         /* initialize random seed: */       srand ( time(NULL) );         total = 0;         newcard = 0;         card1 = rand() % 10 + 1;          card2 = rand() % 10 + 1;          printf("\nyour cards are: %d and %d\n", card1, card2);         total = card1 + card2;         while (1)         {             printf("your total is: %d\n", total);             printf("would you like another? 1=yes 2=no\n");             scanf("%d", &gamechoice);             if (1 == gamechoice)             {                 newcard = rand() % 10 + 1;                 printf("new card is: %d\n", newcard);                 total += newcard;                 if (total == 21)                 {                     printf("21 you win! play again? 1=yes 2=no\n");                     scanf("%d", &gamechoice);                     if (gamechoice == 2)                     {                         looping = 0;                         break;                                }                     break;                 }                 else if (total > 21)                 {                     printf("you have %d\n", total);                     printf("you lose! Play again? 1=yes 2=no\n");                     scanf("%d", &gamechoice);                     if (gamechoice == 2)                     {                         looping = 0;                         break;                               }                     break;                 }             }             else             {                 printf("play again? 1=yes 2=no\n");                 scanf("%d", &gamechoice);                 if (gamechoice == 2)                 {                     looping = 0;                    break;                          }                 break;             }         }     } while (looping);         system("PAUSE");     return 0; }```