
November 19th, 2008, 06:02 PM
|
|
Registered User
|
|
Join Date: Oct 2008
Posts: 9
Time spent in forums: 2 h 25 m 52 sec
Reputation Power: 0
|
|
|
Second part of Program ( Black Jack )
This program is stating that the function "status()" is undefined even though it is defined in the class files.
These are the errors:
Code:
JJAC_B.cpp: In function `char hitStay()':
JJAC_B.cpp:94: `status' undeclared (first use this function)
JJAC_B.cpp:94: (Each undeclared identifier is reported only once for each
function it appears in.)
[J00134003@redhat3 J00134003]$
This is the header:
Code:
#ifndef JJAC_B_H
#define JJAC_B_H
class Blackjack //Class for Blackjack Game
{
public:
void welcome(); //Beginning Message
void blackjack_instructions();
int draw(); //Lets user and dealer draw a random card from the deck
char hitStay(); //Returns if the user wants to choose another card or stay
void getWinner();
void finish(); //End the game(ask the user to play again)
void status(); //If the player is under 21, ask them to hit or stay
void dealerFirst(); //Determine dealer's first card
void playerFirst(); //Determine player's first card
void playAgainDecide(); //Would the user like to play again
};
#endif
This is the cpp file: (Again just compiling for errors)
Code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;
#include "JJAC_B.h"
void Blackjack::welcome()
{
int yesno; //variable for user input
ofstream outBlackJackFile( "blackjack.dat", ios::out );
if( !outBlackJackFile )
{
cerr << "File cannot be opened" << endl;
exit( 1 );
}
//Asks user if they want to play this game
cout << "Welcome to Blackjack. If you are sure that you would like to play this game, please press 1. Otherwise, press 0. ";
cin >> yesno;
//Calls the function that displays instructions
while( yesno != 0 )
{
blackjack_instructions();
//Asks user if they are sure they would like to exit
cout << "Are you sure that you would like to leave this game? To exit press 0: ";
cin >> yesno;
}
}
//Displays instructions for blackjack
void Blackjack::blackjack_instructions()
{
int useranswer; //allows user to continue with game
//Explanation of how to play Blackjack
cout << "\nWelcome to Blackjack. In this game,\n"
<< "cards will be randomly drawn for the deck\n"
<< "for as many times as you would like.\n"
<< "You will be playing against the dealer.\n"
<< "To win, you must get a score of 21 or beat the dealer's score.\n"
<< "If you draw too many cards and get higher than 21,\n"
<< "The dealer wins and vice versa for the dealer.\n"
<< "After each game, you will be told\n"
<< "whether or not you won, lost, or tied with the dealer.\n"
<< "If you would like to begin the game, press 1.\n"
<< "Otherwise, press 0: ";
cin >> useranswer;
//Calls either the function that runs or ends the blackjack game
while( useranswer != 0 )
{
playerFirst();
}
}
//Does the drawing of cards behind blackjack
void Blackjack::playerFirst()
{
//Determine the player's two cards
int playertotal = 0;
int card=draw();
cout << "The card you drew is a " << card << " and a ";;
playertotal+=card;
card=draw();
playertotal+=card;
cout << card << ", so the total is " << playertotal << endl;
status();
}
int draw()
{
int card;
card = 1+rand()%11;
return card;
}
char hitStay()
{
char move;
cout << "\nWould you like to hit (h) or stay (s)? ";
cin >> move;
if (move=='h'||move=='H'||move=='s'||move=='S') //allows for lower or uppercase letters
{
status();
return move;
}
else
{
cout << "Please enter h or s" << endl;
hitStay();
}
}
void Blackjack::status()
{
int playertotal;
int card;
char move;
while (playertotal<21)
{
move=hitStay();
if( move =='s' ) //If the player wants to stay with the card they have...
{
dealerFirst();
}
else if (move=='h') //If the player wants to add another card...
{
playertotal+=draw();
cout << "\tThe card you drew is " << card << ". Your total is " << playertotal << "." << endl;
}
}
finish();
}
void Blackjack::dealerFirst()
{
int dealertotal;
int playertotal;
int card;
card = draw();
dealertotal += draw();
cout << "Dealer drew: " << card << endl;
if (playertotal>21)
finish();
while (dealertotal<=21)
{
dealertotal+=draw();
cout << "\tDealer has" << card << ", total is " << dealertotal << "..." << endl;
hitStay();
}
}
void Blackjack::finish()
{
getWinner();
playAgainDecide();
}
void Blackjack::playAgainDecide()
{
int playAgain;
cout << "Would you like to play again (y/n) ";
cin >> playAgain;
if (playAgain=='Y' ||playAgain=='y') //allows for upper and lower case input
{
playerFirst();
}
else if (playAgain=='N' || playAgain=='n') //allows for upper and lower case input
{
cout << "Thanks for playing!\n\n" << endl;
exit(0);
}
else
{
cout << "Please enter y or n. " << endl;
playAgainDecide();
}
}
void Blackjack::getWinner()
{
int playertotal;
int dealertotal;
if (playertotal>21)
{
cout << "Sorry, you lose. The dealer wins this game." << endl;
}
else if (dealertotal>21)
{
cout << "The dealer loses with " << dealertotal << ", so you win this game." << endl;
}
else
{
if (playertotal>dealertotal)
{
cout << "Your " << playertotal << " beats the dealer's " << dealertotal << ". Congratulations!\n" << endl;
}
else if (playertotal<dealertotal)
{
cout << "Your " << playertotal << " loses to the dealer's " << dealertotal << ". Sorry!\n" << endl;
}
else
{
cout << "It is a tie! Both you and the dealer have " << playertotal << ".\n" << endl;
}
}
}
If you can aid in this situation. I would very much appreciate it.
Last edited by CDCAREY089 : November 19th, 2008 at 06:38 PM.
Reason: Changed program around
|