While loop won't initialize, only if statement will.

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 3873 Views - Last Post: 17 July 2012 - 10:28 PM Rate Topic: -----

#1 JoePro8011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-July 12

While loop won't initialize, only if statement will.

Posted 12 July 2012 - 02:38 PM

Hi, I'm having trouble with this school project. It's supposed to be a simple blackjack program. It's sort of a crash course, and I kind of dozed off when 'Arrays' were taught. So be easy with me. Now a while loop, day 1 stuff, isn't working for some reason. The program runs, until I get to the while loop and then it will hang, like if you didn't put getchar() which I tried anyway. But an "if" statement will work. Here's all my code so far;

 #include <stdio.h>
#define PLAYERS_HAND 52
#define DEALERS_HAND 52
int playerCards[PLAYERS_HAND] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,  //Array to initialize
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,  //52 card
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};// 
int dealerCards[DEALERS_HAND] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,  //Array to initialize
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,  //52 card
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};// 
                           
  int getRandomCard1()
  {
    int randomCard, tempCard;
    int x = 0;

    {
    /*for(x = 0; x < 2; x++);
     {
             randomCard = rand() % 51; <<-- was supposed to generate 2 random cards with one getRandomCard() but kept getting the same card, so I just made two, and reran one of the statements when user chose "Hit"
             tempCard = playerCards[randomCard];
     }*/
     }
     return tempCard;
  }
     
     int getRandomCard2()
  {
    int randomCard, tempCard = 0;
    int x = 0;

    {
    while(tempCard == 0) //So a 0 won't be dealt, but it's redundant anyways
     {
             randomCard = rand() % 51;
             tempCard = playerCards[randomCard];
     }
     }
     return tempCard;
  }   
 int main(void)
{  

   srand (time(0));
  int dealerCardOne, dealersHand, dealerHiddenCard, dealerTotal, 
  wager, balance, win, lose, nextCard, playerTotal, randomCard1, randomCard2, input;    
 
  printf("How much are you playing with today? ");
  scanf("%d", &balance);

  printf("What is your wager? ");
  scanf("%d", &wager);

  while(wager > balance)
             {
             printf("Sorry, you can't wager %d, you only have %d left\n", wager, balance);
             printf("What is your wager? ");
             scanf("%d", &wager);
             }    
  while(wager < 10)
             {
              printf("Sorry, the minimum bet for this table is 10");
              printf("What is your wager? ");
              scanf("%d", &wager);
              }     
  randomCard1 = getRandomCard1();
  randomCard2 = getRandomCard2();

printf("Your cards are %d, %d\n", randomCard1, randomCard2);
playerTotal = randomCard1/*1?*/ + randomCard2/*2?*/;

printf("Your sum is %d. Would you like to hit(1) or stay(2)? ", playerTotal);
scanf("%d", &input);

          if(input == 1 && playerTotal < 21); //This is where I'm stuck. It will run as is, but I need a while loop.
          {
          randomCard1 = getRandomCard1();
          printf("You got a %d\n", randomCard1);
          playerTotal += randomCard1;
          printf("Your sum is %d. Would you like to hit(1) or stay(2)? ", playerTotal);
          scanf("%d", &input);
          }
    



//It's nowhere near done nor am I looking to get it done right now, it's due in a week. I just need a little bit of help with where I'm stuck.

  system("pause");
  return 0;
}    


Is This A Good Question/Topic? 0
  • +

Replies To: While loop won't initialize, only if statement will.

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,571
  • Joined: 25-December 09

Re: While loop won't initialize, only if statement will.

Posted 12 July 2012 - 02:43 PM

Quote

The program runs

That's strange, when I compile your code I get these errors and warnings:

Quote

main.c|14|warning: no previous declaration for ‘getRandomCard1’|
main.c||In function ‘getRandomCard1’:|
main.c|17|warning: unused variable ‘x’|
main.c|16|warning: unused variable ‘randomCard’|
main.c|29|warning: no previous declaration for ‘getRandomCard2’|
main.c||In function ‘getRandomCard2’:|
main.c|37|error: implicit declaration of function ‘rand’|
main.c|32|warning: unused variable ‘x’|
main.c||In function ‘main’:|
main.c|46|error: implicit declaration of function ‘srand’|
main.c|46|error: implicit declaration of function ‘time’|
main.c|77|warning: suggest braces around empty body in an ‘if’ statement|
main.c|91|error: implicit declaration of function ‘system’|
main.c|48|warning: unused variable ‘nextCard’|
main.c|48|warning: unused variable ‘lose’|
main.c|48|warning: unused variable ‘win’|
main.c|47|warning: unused variable ‘dealerTotal’|
main.c|47|warning: unused variable ‘dealerHiddenCard’|
main.c|47|warning: unused variable ‘dealersHand’|
main.c|47|warning: unused variable ‘dealerCardOne’|
||=== Build finished: 4 errors, 13 warnings ===|


You seem to be missing a couple of header file inclusions and look carefully at the warning about line 77.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,125
  • Joined: 05-May 12

Re: While loop won't initialize, only if statement will.

Posted 12 July 2012 - 02:56 PM

Yup, I couldn't get it to compile, either. Missing headers, mostly.

And about line 77... take a close look at every character on the line, not just the boolean in the parenthesis.

Also take a close look at getRandomCard1(). It looks like you've comment out code, and so it sets up a condition where an uninitialized variable is returned.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,571
  • Joined: 25-December 09

Re: While loop won't initialize, only if statement will.

Posted 12 July 2012 - 03:07 PM

Actually the bolded part below is the key:

Quote

suggest braces around empty body in an ‘if’ statement

Does that if actually have an empty body?

Jim
Was This Post Helpful? 0
  • +
  • -

#5 JoePro8011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-July 12

Re: While loop won't initialize, only if statement will.

Posted 12 July 2012 - 04:25 PM

Oh sorry. Forgot to mention it's a "*.C" file (C source), not C++. Here's an updated code, without the commented out part. I just ran it again, so it does run. Remember to save it as *.C

#include <stdio.h>
#define PLAYERS_HAND 52
#define DEALERS_HAND 52
int playerCards[PLAYERS_HAND] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,  //Array to initialize
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,  //52 card
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};// 
int dealerCards[DEALERS_HAND] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,  //Array to initialize
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,  //52 card
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};// 
                           
  int getRandomCard1()
  {
    int randomCard, tempCard;
    int x = 0;

    {

          randomCard = rand() % 51;
          tempCard = playerCards[randomCard];
     
     }
     return tempCard;
  }
     
     int getRandomCard2()
  {
    int randomCard, tempCard = 0;
    int x = 0;

    {
    while(tempCard == 0)
     {
             randomCard = rand() % 52;
             tempCard = playerCards[randomCard];
     }
     }
     return tempCard;
  }   
 int main(void)
{  

   srand (time(0));
  int dealerCardOne, dealersHand, dealerHiddenCard, dealerTotal, 
  wager, balance, win, lose, nextCard, playerTotal, randomCard1, randomCard2, input;    
 
  printf("How much are you playing with today? ");
  scanf("%d", &balance);

  printf("What is your wager? ");
  scanf("%d", &wager);

  while(wager > balance)
             {
             printf("Sorry, you can't wager %d, you only have %d left\n", wager, balance);
             printf("What is your wager? ");
             scanf("%d", &wager);
             }    
  while(wager < 10)
             {
              printf("Sorry, the minimum bet for this table is 10");
              printf("What is your wager? ");
              scanf("%d", &wager);
              }     
  randomCard1 = getRandomCard1();
  randomCard2 = getRandomCard2();

printf("Your cards are %d, %d\n", randomCard1, randomCard2);
playerTotal = randomCard1/*1?*/ + randomCard2/*2?*/;

printf("Your sum is %d. Would you like to hit(1) or stay(2)? ", playerTotal);
scanf("%d", &input);

          if(input == 1 && playerTotal < 21);//This is where I'm stuck. It will run as is, but I need a while loop.
          {
          randomCard1 = getRandomCard1();
          printf("You got a %d\n", randomCard1);
          playerTotal += randomCard1;
          printf("Your sum is %d. Would you like to hit(1) or stay(2)? ", playerTotal);
          scanf("%d", &input);
          }
    





  system("pause");
  return 0;
}    
    


Was This Post Helpful? 0
  • +
  • -

#6 JoePro8011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-July 12

Re: While loop won't initialize, only if statement will.

Posted 12 July 2012 - 04:32 PM

Do you guys suggest another (free) program beside Dev-C++? I know they're pretty much all the same, but I'm liking the color scheme this site posts better.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,125
  • Joined: 05-May 12

Re: While loop won't initialize, only if statement will.

Posted 12 July 2012 - 05:08 PM

Look closely at your line 75. Is the semicolon supposed to be there?
Was This Post Helpful? 0
  • +
  • -

#8 JoePro8011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-July 12

Re: While loop won't initialize, only if statement will.

Posted 12 July 2012 - 05:31 PM

Ah yes! I caught that right after my last post :)

Here's some updated code. I just gotta mix in the dealers hand vs the players.

#include <stdio.h>
#define PLAYERS_HAND 52
#define DEALERS_HAND 52
int playerCards[PLAYERS_HAND] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,  //Array to initialize
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,  //52 card
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};// 
int dealerCards[DEALERS_HAND] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,  //Array to initialize
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,  //52 card
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};// 
                           
  int getRandomCard1()
  {
    int randomCard, tempCard;
    int x = 0;

    {

          randomCard = rand() % 51;
          tempCard = playerCards[randomCard];
     
     }
     return tempCard;
  }
     
     int getRandomCard2()
  {
    int randomCard, tempCard = 0;
    int x = 0;

    {
    while(tempCard == 0)
     {
             randomCard = rand() % 52;
             tempCard = playerCards[randomCard];
     }
     }
     return tempCard;
  }   
 int main(void)
{  

   srand (time(0));
  int dealerCardOne, dealersHand, dealerHiddenCard, dealerTotal, 
  wager, balance, win, lose, nextCard, playerTotal, randomCard1, randomCard2, input;    
 
  printf("How much are you playing with today? ");
  scanf("%d", &balance);

  printf("What is your wager? ");
  scanf("%d", &wager);

  while(wager > balance)
             {
             printf("Sorry, you can't wager %d, you only have %d left\n", wager, balance);
             printf("What is your wager? ");
             scanf("%d", &wager);
             }    
  while(wager < 10)
             {
              printf("Sorry, the minimum bet for this table is 10");
              printf("What is your wager? ");
              scanf("%d", &wager);
              }     
  randomCard1 = getRandomCard1();
  randomCard2 = getRandomCard2();

printf("Your cards are %d, %d\n", randomCard1, randomCard2);
playerTotal = randomCard1/*1?*/ + randomCard2/*2?*/;

printf("Your sum is %d. Would you like to hit(1) or stay(2)? ", playerTotal);
scanf("%d", &input);

          while(input == 1)
          {
          randomCard1 = getRandomCard1();
          playerTotal += randomCard1;
                  if(playerTotal > 21)     
                      {
                                     printf("You got a %d, your sum is %d. Sorry, you lose.\n", randomCard1, playerTotal);
                                     input = 2;
                      }     
                  else if(playerTotal < 21)          
                      {
                                          printf("You got a %d\n", randomCard1);
                                          printf("Your sum is %d. Would you like to hit(1) or stay(2)? ", playerTotal); 
                                          scanf("%d", &input);
                      }
                  else if(playerTotal = 21)
                      {
                      printf("You got a %d. You got Blackjack! You win!\n", randomCard1);
                      printf("Would you like to play again? \n");
                      scanf("%d", &input);
                      }            
          }
    





  system("pause");
  return 0;
}    
    


Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,571
  • Joined: 25-December 09

Re: While loop won't initialize, only if statement will.

Posted 12 July 2012 - 08:20 PM

How does this code compile? You are missing several include files. Here are the errors I receive when I compile your code:

Quote

main.c|13|warning: no previous declaration for ‘getRandomCard1’|
main.c||In function ‘getRandomCard1’:|
main.c|20|error: implicit declaration of function ‘rand’|
main.c|16|warning: unused variable ‘x’|
main.c|27|warning: no previous declaration for ‘getRandomCard2’|
main.c||In function ‘getRandomCard2’:|
main.c|30|warning: unused variable ‘x’|
main.c||In function ‘main’:|
main.c|44|error: implicit declaration of function ‘srand’|
main.c|44|error: implicit declaration of function ‘time’|
main.c|90|warning: suggest parentheses around assignment used as truth value|
main.c|103|error: implicit declaration of function ‘system’|
main.c|46|warning: unused variable ‘nextCard’|
main.c|46|warning: unused variable ‘lose’|
main.c|46|warning: unused variable ‘win’|
main.c|45|warning: unused variable ‘dealerTotal’|
main.c|45|warning: unused variable ‘dealerHiddenCard’|
main.c|45|warning: unused variable ‘dealersHand’|
main.c|45|warning: unused variable ‘dealerCardOne’|
||=== Build finished: 4 errors, 12 warnings ===|


You need to include the proper header files for the functions you are using. Check the documentation for rand(), srand(), time(), and system() to see what header files declare these functions. If you are not getting any warnings or errors then you need to check your IDE settings to increase the warning levels. If you are getting warnings, don't ignore them. Having your compiler generate warnings is essential for creating reliable programs.


Jim

This post has been edited by jimblumberg: 12 July 2012 - 08:23 PM

Was This Post Helpful? 0
  • +
  • -

#10 JoePro8011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-July 12

Re: While loop won't initialize, only if statement will.

Posted 12 July 2012 - 08:50 PM

Like I said, it is a *.C (C source file). It will not compile if saved as C++. It compiles and runs without any errors on my end. I'm using Dev-C++. I uploaded the file here http://www.mediafire...oqs9twi1r991r4x
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,125
  • Joined: 05-May 12

Re: While loop won't initialize, only if statement will.

Posted 12 July 2012 - 08:56 PM

View PostJoePro8011, on 12 July 2012 - 08:50 PM, said:

Like I said, it is a *.C (C source file). It will not compile if saved as C++. It compiles and runs without any errors on my end. I'm using Dev-C++. I uploaded the file here http://www.mediafire...oqs9twi1r991r4x


And if you notice the errors list that Jim shows, he is compiling your source code as "main.c" and still getting the errors and warnings.
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,571
  • Joined: 25-December 09

Re: While loop won't initialize, only if statement will.

Posted 12 July 2012 - 08:58 PM

Like I said you need to include the proper include files. I did try to compile this with a C compiler. It will not compile for me, because I have much higher error and warning levels set with my compiler. If you don't set your compiler to generate warnings then it will compile but it will not run correctly. When you use standard functions and don't include the proper headers C will make some assumptions about these functions that are usually incorrect. For example the compiler will assume that the function returns an int.

You need to change your compiler settings to increase your warning levels. By default Dev-C++ doesn't issue any warnings.

Also for your information, the reason this doesn't compile for you as C++ program is because C++ insists that you supply the proper header files for the functions you use. When I add the proper include files in a C++ program I get these warnings:

Quote

main.cpp||In function ‘int getRandomCard1()’:|
main.cpp|16|warning: no previous declaration for ‘int getRandomCard1()’ [-Wmissing-declarations]|
main.cpp|19|warning: unused variable ‘x’ [-Wunused-variable]|
main.cpp||In function ‘int getRandomCard2()’:|
main.cpp|30|warning: no previous declaration for ‘int getRandomCard2()’ [-Wmissing-declarations]|
main.cpp|33|warning: unused variable ‘x’ [-Wunused-variable]|
main.cpp||In function ‘int main()’:|
main.cpp|93|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
main.cpp|48|warning: unused variable ‘dealerCardOne’ [-Wunused-variable]|
main.cpp|48|warning: unused variable ‘dealersHand’ [-Wunused-variable]|
main.cpp|48|warning: unused variable ‘dealerHiddenCard’ [-Wunused-variable]|
main.cpp|48|warning: unused variable ‘dealerTotal’ [-Wunused-variable]|
main.cpp|49|warning: unused variable ‘win’ [-Wunused-variable]|
main.cpp|49|warning: unused variable ‘lose’ [-Wunused-variable]|
main.cpp|49|warning: unused variable ‘nextCard’ [-Wunused-variable]|
||=== Build finished: 0 errors, 12 warnings ===|

Notice the file name, this is compiled with g++ as a C++ program.


Jim

This post has been edited by jimblumberg: 12 July 2012 - 09:04 PM

Was This Post Helpful? 0
  • +
  • -

#13 JoePro8011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-July 12

Re: While loop won't initialize, only if statement will.

Posted 12 July 2012 - 10:05 PM

Okay, besides the unused variables (I havn't used them yet, the code isn't finished) could you break down the other errors for me?
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,571
  • Joined: 25-December 09

Re: While loop won't initialize, only if statement will.

Posted 12 July 2012 - 10:44 PM

The following two errors:

Quote

main.c|44|error: implicit declaration of function ‘srand’|
main.c|44|error: implicit declaration of function ‘time’|

Are both telling you that the compiler could not find a declaration/prototype for these functions, meaning you forgot to include the proper include file. You need to read the documentation for any function you use and insure you include the proper include file for that function.

For the next warning, can you tell me what you think it is telling you?

Quote

main.c|90|warning: suggest parentheses around assignment used as truth value|

Can you tell me what line this waring is referring to? Please show me the line and tell me what you think is wrong with this line.

Several of the other errors are because my compiler wants you to properly prototype your functions before you use them. For example:

Quote

main.c|13|warning: no previous declaration for ‘getRandomCard1’|

This not really a problem since you have implemented this function prior to any other function that uses it. But it is a good practice to always supply function prototypes for your functions. This will making moving to multiple source files and header files easier.

It is very important that you learn to read your error messages, these messages while sometimes cryptic at present will start to make more and more sense as you continue on your learning path.

Jim
Was This Post Helpful? 0
  • +
  • -

#15 JoePro8011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-July 12

Re: While loop won't initialize, only if statement will.

Posted 12 July 2012 - 11:25 PM

else if(playerTotal *==* 21)


I caught that earlier. 'srand' and 'time' I was told to use those to generate a random number. I fixed a bunch of things around. It's coming along slowly. I couldn't find the option to alert me of all warnings, though. I'm having trouble trying to tell the program not to repeat the same card, per deck. Plus a shuffle after a new hand, and a little bit more tweeking and I think I'm finished.
Here's what I have so far:
 
#include <stdio.h>
#define PLAYERS_HAND 52
#define DEALERS_HAND 52
int playerCards[PLAYERS_HAND] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,  //Array to initialize
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,  //52 card
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};// 
int dealerCards[DEALERS_HAND] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,  //Array to initialize
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,  //52 card
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};// 
                           
 int getRandomCard1()
     {
          int randomCard, tempCard;
          {
          randomCard = rand() % 51;
          tempCard = playerCards[randomCard];
          }
          return tempCard;
     }
 

 int main(void)
{  

   srand (time(0));
  int dealerTotal, wager, balance, playerTotal, randomCard1, input, replay = 1;
  int x;    
 
  printf("How much are you playing with today? ");
  scanf("%d", &balance);
while(replay == 1)
{
playerTotal = 0, dealerTotal = 0;
  printf("What is your wager? ");
  scanf("%d", &wager);

  while(wager > balance)
             {
             printf("Sorry, you can't wager %d, you only have %d left\n", wager, balance);
             printf("What is your wager? ");
             scanf("%d", &wager);
             }    
  while(wager < 10)
             {
              printf("Sorry, the minimum bet for this table is 10");
              printf("What is your wager? ");
              scanf("%d", &wager);
              }     
  randomCard1 = getRandomCard1();

printf("Your cards are ");
    for(x = 0; x < 1; x++)
    {
        randomCard1 = getRandomCard1();
        printf("%d, ", randomCard1);
        playerTotal += randomCard1;
    }
        for(x = 0; x < 1; x++)
    {
        randomCard1 = getRandomCard1();
        printf("%d\n", randomCard1);
        playerTotal += randomCard1;
    }


printf("Your sum is %d. Would you like to hit(1) or stay(2)? ", playerTotal);
scanf("%d", &input);

          while(input == 1)
          {
          randomCard1 = getRandomCard1();
          playerTotal += randomCard1;
                  if(playerTotal > 21)     
                      {
                      balance -= wager;
                                     printf("You got %d.\nYour sum is %d. You lose the game. You have $%d left.\n", randomCard1, playerTotal, balance);
                                     input = 2;
                      }     
                  else if(playerTotal < 21)          
                      {
                                          printf("You got a %d\n", randomCard1);
                                          printf("Your sum is %d. Would you like to hit(1) or stay(2)? ", playerTotal); 
                                          scanf("%d", &input);
                      }
                  else if(playerTotal == 21)
                      {
                      balance += wager;
                      printf("You got %d. You got Blackjack! You win!\nYou have $%d left.\n", randomCard1, balance);
                      }            
          }
          printf("Computer's cards: ");
          for(x = 0; x < 3; x++)
          {
                randomCard1 = getRandomCard1();
                printf("%d, ", randomCard1);
                dealerTotal += randomCard1;
          }
          for(x = 0; x < 1; x++)
          {
                randomCard1 = getRandomCard1();
                printf("%d ", randomCard1);
                dealerTotal += randomCard1;
          }
          printf("Computer got: %d\n", dealerTotal);
          
          if(balance >= 10)
          {
                     printf("Would you like to play again? Yes(1) or No(2)? ");
                     scanf("%d", &replay);
          }
          else
          {
              printf("Thank you for playing. Good bye.\n");
              replay = 2;
          }

}



  system("pause");
  return 0;
}    
    


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2