Like Tree1Likes
  • 1 Post By laserlight

My program isnt accurate

This is a discussion on My program isnt accurate within the C Programming forums, part of the General Programming Boards category; Programming Assignment Write a modified version of Blackjack. This modified card game has cards numbered 1- 10. The player and ...

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

    Help please, my output is incorrect

    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]

    #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;
    }
    Attached Files Attached Files
    Last edited by bcd6f; 10-31-2012 at 05:19 PM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,497
    This should be moved to the C++ board
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,563
    Looks like you didn't read up on how to post in our forums. Here's some links to read that will help you:
    Announcements - General Programming Boards
    Announcements - C Programming

    Also, your code is C++, but you posted in the C forum. Hopefully a moderator will be kind enough to move it.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    ok i changed it thank you

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,563
    Quote Originally Posted by bcd6f View Post
    ok i changed it thank you
    Not quite, or at least not completely/correctly. You need a closing [/code] tag as well, at the end of everything. And you should make sure your code is nicely formatted and indented. Many people here wont help you if your code is poorly formatted.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    Re-posted in C++ programming forum: help please, my output is slightly wrong

    *thread closed*
    bcd6f likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 10-31-2011, 11:06 AM
  2. Program isnt entering the If statements.
    By ceptyoubestbud in forum C Programming
    Replies: 20
    Last Post: 08-14-2011, 08:36 AM
  3. Pascal's Triangle Program Only Accurate to 12 Rows
    By quintaessentia in forum C Programming
    Replies: 6
    Last Post: 03-29-2010, 04:12 PM
  4. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  5. VERY accurate timing
    By Gherkin in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-28-2001, 04:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21