Hi, I only have a couple of hours to send this file for a grade but it is not compiling. Could anyone please help me get my program to work? I don't want to come off as desperate but coming to the internet was my last resort.
I am creating two games in one program which are the Games of 21 and heads and tails. The heads and tails game compiles when it is separate but the game of 21 gives me the same compiling errors which are
130 undefined reference to `play21()'
145 undefined reference to `dealCards(int, std::string)'
147 undefined reference to `dealCards(int, std::string)'
151 undefined reference to `hit(int&)'
156 undefined reference to `dealCards(int, std::string)'
189 undefined reference to `Random(int, int)'
[Error] ld returned 1 exit status
if anyone could find a solution, it would be much appreciated, Thank you.
Here is my program.
#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib> //for rand and srand
#include <ctime> //for time function
using namespace std;
const string PLAY_GAME = "Enter your name: ";
const string BYE = "See you again soon!";
void play21(void);
int dealCards(int, string);
void hit(int &);
int determineWinner(int, int);
int Random(int, int);
int humanScore;
int houseScore;
int playerScore;
int main()
{
char ans;
char name;
cout << "Do you want to play? (Enter Y/y for yes or N/n for no) \n";
cin >> ans;
cout << "Choose which game you want to play (press 1, 2 or 3) \n 1) Heads or Tails \n 2) Game of 21 \n 3) Quit \n " << endl;
cin >> choice;
if (choice == 1)
{
char guess;
int no_of_plays;
const int LOW = 0;
const int HIGH = 1;
int random_integer;
char ans;
cout << "WELCOME TO THE GAME OF HEADS OR TAILS!" << endl;
int point = 0;
int counter = 0;
ans = 'Y';
while (ans == 'Y' || ans == 'y' )
{
cout << "Do you want to continue? Yes = Y/y and No = N/n" << endl;
cin >> ans;
if(ans == 'Y' || ans == 'y'){
cout << "Call it, Heads or Tails?" << endl;
cout << "h for heads t for tails" << endl;
}
cout << "you have " << point << " points"<< endl;
}
cout << "Thanks for playing";
}
if (choice == 2)
{
while (ans == 'Y' || ans == 'y' )
{
cout << "WELCOME TO THE GAME OF 21!" << endl;
cout << "Ready to play? " << endl;
cout << "Y/y for yes, N/n for no " << endl;
cin >> ans;
{
int keepPlaying = 0; //loop control variable
cout << "\tSimple game of 21\n\n";
cout << "The person closest to 21 wins!\n\n";
do
{
play21();
cout << "\nDo you want to play another hand?\n";
cout << "Press 1 for Yes\n";
cout << "Press 0 for No\n";
cin >> keepPlaying;
}while(keepPlaying == 1);
return 0;
}
}
void play21(void);
{
int person = dealCards(2, "\nYour 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;
while ((house < person) && (house <= 21) && (person <= 21))
{
house += dealCards(1, "The Computer takes a card: ");
cout << "= " << house << endl;
}
}
int determineWinner(int humanScore, int houseScore);
{
if (humanScore > houseScore && humanScore < 22)
{
cout << "You win!\n";}
else if (humanScore < houseScore && houseScore < 22){
cout << "Computer wins!\n";}
else if (humanScore > 21 && houseScore < 22){
cout << "You both busted! No winner!\n";}
else if (houseScore > 21){
cout << "Computer busted! You win!\n";}
else if (humanScore > 21){
cout << "Player busted! Computer wins!\n";}
else{
cout << "It was a tie.\n";
}
int dealCards;
int numberOfCards;
string message;
int totalValue;
int cardValue;
cout << message;
for (int i = 1; i <= numberOfCards; i++)
{
cardValue = Random(10, 1);
cout << cardValue << " ";
totalValue = totalValue + cardValue;
}
return totalValue;
}
void hit(int &playerScore);
{
char playerHit = 'y';
while ((playerHit == 'y') || (playerHit == 'Y'))
{
if (playerScore <= 21){
cout << "Do you want to hit? (y/n) "<<endl;
cin >> playerHit;
}
}
}
}
if (choice == 3)
{
cin >> choice;
cout << "Thank you! Have a nice day!\n";
I had a hard time reading your code so I went and organized it a bit. Most of those functions were undefined references because you had not even defined them. Others were undefined because the code was messy and led to stray and sometimes backwards braces. Also, the last if statement is in the wrong function so I moved it. Oh, and in your function definitions, there should be no semicolon. Here is the organized code...
It's a bit difficult because the player and computer is represented by an integer. In the future, try and use classes, as you can have several member functions and variables to represent the player's hand and point value. However, since you only have a few hours, I'd suggest storing the card values in a string array. Don't worry about the suits for now. And when you write your hit function... take input from the user rather than initializing the playerHit variable to 'y' on creation.
Adding the function definitions is simply copying the prototype, removing the semicolon, and adding the body { } for it. You can place it into a separate source file (recommended if you're lone source code file is getting too unwieldy).
Hope that helps. If you need more help, or some tutoring, let me know, I'd be happy to offer any assistance.