Thread: My program isnt accurate

  1. #1
    Registered User
    Join Date
    Oct 2012

    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.


    #include <iostream>
    #include <ctime>
    #include <string>

    using namespace std;

    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 {

    //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...
    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;
    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;
    value = Random(0,10);
    cout << value << " ";
    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()
    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
    Melbourne Australia
    This should be moved to the C++ board
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    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
    ok i changed it thank you

  5. #5
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    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
    Re-posted in C++ programming forum: help please, my output is slightly wrong

    *thread closed*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 12:07 PM
  5. VERY accurate timing
    By Gherkin in forum A Brief History of
    Replies: 2
    Last Post: 09-28-2001, 04:30 PM