Grade Depends on This ASAP please!

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;

(ans == 'Y' || ans == 'y');

{
cout << PLAY_GAME;
cin >> name;


string filename ("gamers.txt");
cin >> filename;
}



int choice;

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;

cin >> guess;
char randomCoin;

random_integer = rand() % (HIGH - LOW + 1) + LOW;
if (random_integer == 1)

{

cout << "Coin was tails" << endl;
randomCoin = 't';
}
if (random_integer == 0)
{
cout << "Coin was heads" << endl;
randomCoin = 'h';

}

if(guess == randomCoin){
counter = counter + 1;
cout << "You got it!" << endl;

}
else
{
counter = 0;
cout << "You missed it!" << endl;

}
if(counter == 2){
counter = 0;

point = point + 2;

}

}
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";

}

system("pause");
return 1;
}
Last edited on
"undefined reference" means the compiler can't find the definition of the function in question.
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...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#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;

    if (ans == 'Y' || ans == 'y');
    {
        cout << PLAY_GAME;
        cin >> name;
        string filename ("gamers.txt");
        cin >> filename;
    }

    int choice;

    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;

                cin >> guess;
                char randomCoin;

                random_integer = rand() % (HIGH - LOW + 1) + LOW;
        
                if (random_integer == 1)
                {
                    cout << "Coin was tails" << endl;
                    randomCoin = 't';
                }
               
                if (random_integer == 0)
                {
                    cout << "Coin was heads" << endl;
                    randomCoin = 'h';
                }

                if(guess == randomCoin)
                {
                    counter = counter + 1;
                    cout << "You got it!" << endl;
                }
                
                else
                {
                    counter = 0;
                    cout << "You missed it!" << endl;
                }
                
                if(counter == 2)
                {
                    counter = 0;
                    point = point + 2;
                }

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

    if (choice == 3)
    {
        cin >> choice;
        cout << "Thank you! Have a nice day!\n";
    }
    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;
        }
    }

    system("pause");
}
All you need to do now is add those function definitions.
Last edited on
And you may not even need to define Random(). If you just want a random number, try http://www.cplusplus.com/reference/cstdlib/rand/
How would you go about on adding the function definitions to the code ForTheReallys ? Because I don't know what to do from there.
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.

Joe
sparkprogrammer@gmail.com
Topic archived. No new replies allowed.