C++ Card Game 21

Error with 21 card game

Page 1 of 1

1 Replies - 6965 Views - Last Post: 31 March 2008 - 10:07 PM Rate Topic: -----

#1 Entropicvamp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 31-March 08

C++ Card Game 21

Posted 31 March 2008 - 09:31 PM

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.
#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);
}





Is This A Good Question/Topic? 0
  • +

Replies To: C++ Card Game 21

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: C++ Card Game 21

Posted 31 March 2008 - 10:07 PM

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.

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.

:)

This post has been edited by Martyr2: 31 March 2008 - 10:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1