Thread: help please, my output is slightly wrong

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    help please, my output is slightly wrong



    Programming Assignment

    Write a modified version of Blackjack. This modified card game has cards numbered 1-
    10. The player and dealer take turns, each receiving a card. The cards dealt should
    alternate, between player and dealer, with the player receiving the first card, the dealer
    receives the second card, etc. The program should then print out the player’s hand, as
    well as only one of the dealer’s cards. The program prompts the user for another card.
    The user can choose up to five cards total. After the user has finished “hitting” (the act of
    getting another card), then the computer will deal its own hand, until either it arrives at
    17, or busts by going over 21. Assume a “bottomless” deck.
    If the user busts, display a message saying as much; don’t worry about printing out the
    dealer’s hand. Otherwise, print out the dealer’s hand, and look at the possible options:
    The dealer goes over 21 – the computer busts, and the player wins.
    The player’s cards total more than the dealer’s cards – the player wins.
    The dealer’s cards total more than the player’s cards – the dealer wins.
    The player’s cards total the same as the dealer’s cards – it is a tie game.
    Finally, prompt the user if he/she would like to play again.








    Code:
    
    
    Code:
    #include <iostream>#include <ctime>
    #include <string>
    
    
       using namespace std;
    
    
    //prototypes...
       void blackjack();
       int dealCards(int, string);
       void hit(int &);
       void determineWinner(int, int);
       int Random(int, int);
    
    
    
    
       int main(){
       
          char keepPlaying = 'n'; //loop control variable
       
          do {
             blackjack();
              
          
          //keep playing?
             cout << "Would you like to play again (y/n)?";
             cin >> keepPlaying;
          } while(keepPlaying == 'y');
         
          return 0;
         
       }
    
    
       void blackjack(){
            //play one hand of 21
       
            //randomize the cards
          srand((int) time(0));
       
           // deal the cards
          int person = dealCards(2, "Your Cards:");
          cout << " = " << person << endl;
          int house = dealCards(2, "Computers Cards:");
          cout << " = " << house << endl;
       
            // Ask if human wants a hit and keep hitting...
          hit(person);
          cout << endl;
       
           //Determine if computer takes a hit
          while ((house < person) && (house <= 21) && (person <= 21))
          {
             house += dealCards(1,"The Computer takes a card ");
             cout << endl;
          }
       
           //show who won....
          determineWinner(person, house);
       }
    
    
       void determineWinner(int yourScore, int dealerScore)
       //Compare the scores to see who won
       {
          if (yourScore == 21)
             cout << "You have 21. You win!" << endl;
          else if ((yourScore < 21) && (yourScore > dealerScore))
             cout << "Dealer busts; You win!" << endl;
          else 
             cout << "You lose" << endl;
       }
    
    
    
    
    
    
       int dealCards(int numberOfCards, string message)
       
       //This function deals the cards
       {
       
          int return_value = 0;
          int value = 0;
       
          for (int b = 0; b <= numberOfCards; b++)
          {
          
          
             int cards = b; 
             while(cards--)
             {
                value = Random(0,10); 
                cout << value << " "; 
                if(cards) 
                   cout << " , ";
                return_value += value;
             }
          
          }
          return return_value;
       
       }
    
    
    
    
       void hit(int &playerScore)//This function asks you if you want another card -- 'a hit'
       {
          int cardCount = 0;
          char wantCard = "y" || "n";
          int cardTotal = 0;
          cardTotal = playerScore;
          cout << "Would you like another card?"; 
          while (wantCard == 'y')
          {
             if ((cardTotal > 0 ) && (cardTotal < 21))
                ;
             cout << "Do you want another card?";
             cin >> wantCard;
             if (wantCard == 'y')
                cout << cardTotal + dealCards(1, "You take a card."); // adds yourScore to dealCard()
             else 
                cout << "You decide to stay";
              
             if (cardTotal > 21) 
                cout << "You have gone over 21, You Lose";
          }
       }
            
    
    
    
    
       int Random(int upperbound, int lowerbound)
       {
          int result;
       //returns a random number within the given boundary
          result=    1 + rand() % (upperbound - lowerbound + 1);
       
          return result;
       }


    [code]

    my output:

    7 7 , 2 = 16
    9 8 , 2 = 19
    Would you like another card?
    You lose
    Would you like to play again (y/n)?
    [/code}

    Code:
    supposed to be:
    
    Your hand: 2 5
    Dealer's hand: 6
    Would you like another card? (y/n) y
    Your hand: 2 5 5
    Would you like another card? (y/n) y
    Your hand: 2 5 5 6
    Would you like another card? (y/n) n
    Dealer's hand: 6 8 10
    Dealer busts; you win
    Would you like to play again? (y/n)
    Last edited by bcd6f; 10-31-2012 at 05:45 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char wantCard = "y" || "n";
    Does this generate any compiler warnings?

    > cardTotal = playerScore;
    You should be updating playerScore with the dealt cards.

    > if ((cardTotal > 0 ) && (cardTotal < 21))
    > ;
    Use braces, if you want to enclose a block of code.
    This ; does nothing.

    > cout << cardTotal + dealCards(1, "You take a card."); // adds yourScore to dealCard()
    Adds, but only for the cout statement.
    You need to actually update total.

    > if (cardTotal > 21)
    > cout << "You have gone over 21, You Lose";
    Perhaps force the loop to exit here?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Thank you for that. I turned the assignment in already, now i know.. I appreciate your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slightly Disoriented.
    By CrazyNorman in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-11-2005, 12:24 PM
  2. Slightly depraved humor
    By misplaced in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 04-29-2005, 01:34 PM
  3. a slightly different triangle
    By sweetly in forum C++ Programming
    Replies: 7
    Last Post: 10-13-2003, 01:46 AM
  4. wrong output...
    By _Need-Your_Help in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 12:38 PM
  5. Slightly complicated array&memory questions
    By Boksha in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2002, 11:35 AM