Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,926 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,805 people online right now. Registration is fast and FREE... Join Now!




C++ Card Game 21

 
Reply to this topicStart new topic

C++ Card Game 21, Error with 21 card game

Entropicvamp
31 Mar, 2008 - 08:31 PM
Post #1

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 14


My Contributions
I keep getting the error: 'dealCards' : must return a value, but I dont know where I am going wrong this seems to be my only error in the program, can someone please let me know where I am wrong with this, my luck it's a simple over sight or something stupid I perhaps left out, thanks.
CODE
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

//prototypes...
void play21(void);
int dealCards(int, string);
void hit(int &);
void determineWinner(int, int);
int Random(int, int);


void main(){

       char keepPlaying = 'n'; //loop control variable

       do {
              play21();
          
              //keep playing?
             cout << "Do you want to play anouther hand (y/n)?";
             cin >> keepPlaying;
     } while(keepPlaying == 'Y' || keepPlaying == 'y');
}

void play21(void){
        //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 humanScore, int houseScore) {
    while ((humanScore <= 21) && (humanScore < 0)) //Compare the scores to see who won
        if (humanScore == 21)
        {
            cout << "You have 21.  You win!" << endl;
        }
        else if ((humanScore < 21) && (humanScore > houseScore))
        {
            cout << "You have the closer hand to 21.  You win!"  << endl;
        }

//possible outcomes: human wins, computer wins, tie

}

int dealCards(int numberOfCards, string message){
    for (int a = 0; a <= numberOfCards; a++)  //This function deals the cards
    {
        //Random();
    }
        
}//Here is where I get the error code: 'dealCards' : must return a value    


void hit(int &playerScore){
    char wantCard = 'n';
    //int cardCount = 0;
    int cardTotal = 0;
        cardTotal = playerScore;

        cout << "Would you like another card?";        
        {while (wantCard == 'Y' || 'y')
        
            if ((cardTotal > 0 ) && (cardTotal <= 21))
            {
                //cardCount += 1;
                //cardTotal += Random();
                cout << " " << cardTotal << endl;
                cout << "Would you like another card?";
                cin >> wantCard;
            }
            else
            {
                cout << "You are over 21. You loose!" << endl;
            }
            
        }
        

}

int Random(int lowerLimit, int upperLimit) {
    
//returns a random number within the given boundary
         return 1 + rand() % (upperLimit - lowerLimit + 1);
}



User is offlineProfile CardPM
+Quote Post

Martyr2
RE: C++ Card Game 21
31 Mar, 2008 - 09:07 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Hi Entropicvamp,

The array dealcards is defined there as returning an integer. Notice the first part of the function that reads "int". So in order to return a value from the function, you have to use.... the return statement.

What you will need to do is have this function come up with the cards from the deck and perhaps add them together to formulate an integer score. For instance if you pull an ace and a 10, you would return 21.

cpp

int dealCards(int numberOfCards, string message){
int sumOfCards = 0;

//Do some stuff in here to get the card values
//Set the variable sumOfCards to the value calculated by random cards

return sumOfCards;

}


Notice from the example above that we use the return statement to tell the function to return the value stored in the sumOfCards variable. Also notice that the type of value we return also matches the type returned by the function (sumOfCards is an int variable and the function shows that it must return an int).

If your function has a return type (that is the int on the front of the function) of anything but void, you are going to need to use the return statement.

If the function won't do anything but print or manipulate data and is not designed to return a value to the calling function, then you would use the return type "void".

Read up on return types for functions and the return statement for more information. This will solve your error.

smile.gif

This post has been edited by Martyr2: 31 Mar, 2008 - 09:09 PM
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 10:23PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month