0

I am getting an error in my C++ code. I am generally a Java Developer, but I am taking a class on C++ to be a little more broad in my programming capabilities. Anyway when I try to build this solution, I get the error

1>c:\users\westlakes\documents\visual studio 2010\projects\blackjack\blackjack\playblackjack.cpp(81): error C3861: 'initializeGame': identifier not found
1>c:\users\westlakes\documents\visual studio 2010\projects\blackjack\blackjack\playblackjack.cpp(82): error C3861: 'deal': identifier not found
1>c:\users\westlakes\documents\visual studio 2010\projects\blackjack\blackjack\playblackjack.cpp(83): error C3861: 'playGame': identifier not found

can anyone help me out?

// Programmer:  Matt Westlake

// Course:  COMP220

// Assignment:  Two-Dimensional Arrays

// Description:  The program will use a 2D array and a random-number
// generation to play Blackjack and keep track of a playing-card deck.

// Input:  User data entry and a playing-card deck represented as a two-
// dimensional array

// Output:  A screen display showing the current card hands of each player
// and the dealer, their score, win and lose status, and a final representation
// of the card deck after the game is over

#include <iostream>
#include <iomanip>
#include <windows.h>

using namespace std;

char cPlay = 'Y';       //Character variable for play game input
char tempDealerHand;
char cCardDeck[4][13];  //Character array representing the card deck
char output[5][8];
char card[1][1];
int iCard;              //Card array index
char cardName;
//0 = 2 card                                                              
//1 = 3 card
//2 = 4 card
//3 = 5 card
//4 = 6 card
//5 = 7 card                                                           
//6 = 8 card                                                        
//7 = 9 card                                     
//8 = 10 card                                                       
//9 = jack card
//10 = queen card
//11 = king card
//12 = ace card
int iNumberOfDraws = 0; //Number of rounds of card draws
int iSuit;//Suit array index
char Suit [4]; 

 //0 = diamonds
//1 = hearts
//2 = clubs
//3 = spades

// ASCII character display reference for display card suit symbols
//3 = heart symbol
//4 = diamond symbol
//5 = club symbol
//6 = spade symbol

int iNumberOfPlayers = 0;//Number of players in current game
int iPlayerCount[5];      //Integer array to holder each player's count
                                   //iPlayer[0] is always the dealer
int iHighestCount = 0;  //Highest count for a single game
int k, m;//integer loop counters


void initializeGame();
void deal();
void playGame();
char generateCard();
void generateDisplay();


void initializeGame()
{
if (iNumberOfPlayers == 0)
{
    cout << "No players in game, exiting program";
    Sleep(3000);
    exit(0);
}

cout << iNumberOfPlayers + "Joining the game with the dealer" << endl;
output[0][0] = 'Deal';
char name;
for (int i = 1; i <= iNumberOfPlayers; i++)
{
    output[i][0] = 'P ' + i;
}
for (int i = 0; i < 4; i++)
{
    for (int j = 0; j < 13; j++)
    {
        cCardDeck[i][j] = ' ';
    }
}

srand(GetTickCount());  //Seed the random-number generator
}

void deal()
{
for (k = 0; k <= iNumberOfPlayers; k++)
{
    for (m = 1; m <= 2; m++)
    {
        output[k][m] =  generateCard();
    }
}
tempDealerHand = output[0][0];
// output[0][0] = ? // to set dealer first hand to unknown
generateDisplay();
}

void playGame()
{
char HorS;
for (k = 1; k <= iNumberOfPlayers; k++)
{
    for (m = 3; m <= 5; m++)
    {
        cout << "Player " << k << " Would you like to Hit or Stay? Type H for Hit or anything else for Stay: ";
        cin >> HorS;
        if (HorS == 'H' || HorS == 'h')
        {
            output[k][m] = generateCard();
            generateDisplay();
        }
        else
        {
            break;
        }
    }
}
int dealerDraw = rand() % 3; // dealer will draw 0 to 3 cards;
for (int i = 0; i < dealerDraw; i++)
{
    output[0][i+3] = generateCard();
    generateDisplay();
}
}

char generateCard()
{
iSuit = rand() % 4;
iCard = rand() % 13;
if (iCard <= 8)
{
    cardName = iCard + 2;
}
else if (iCard == 9)
{
    cardName = 'J';
}
else if (iCard == 10)
{
    cardName = 'Q';
}
else if (iCard == 11)
{
    cardName = 'K';
}
else if (iCard == 12)
{
    cardName = 'A';
}
card[0][0] = Suit[iSuit],cardName;
return card[0][0];
}

void generateDisplay()
{ 
for (int i = 0; i < iNumberOfPlayers; i++)
{

    for (int j = 0; j < 8; j++)
    {
    cout << output[i][j];
    }
    cout << endl;
}
}

void main (void)
{


cout << "Do you want to play blackjack?";
cin >> cPlay;
Suit [0] = '0x03';
Suit [1] = '0x04';
Suit [2] = '0x05';
Suit [3] = '0x06';

cout << "Enter number of players: ";
cin >> iNumberOfPlayers;

while (true)
{
    initializeGame();
    deal();
    playGame();
}
}

edit changed so that main was on the bottom, but still getting the error.

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
Matt Westlake
  • 3,079
  • 5
  • 32
  • 74
  • 2
    Unlike in Java, in C++ you can't use functions before you declare them. I'll suggest you just don't assume anything you know about Java applies to C++, because doing so will only lead to pain. – R. Martinho Fernandes Jul 12 '12 at 10:18
  • I changed it so that main() was on the bottom, but it still gives me the error – Matt Westlake Jul 12 '12 at 10:25
  • Because you still don't understand the problem :). In C and C++ you need to declare identifiers before you use them. See http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration/1410632#1410632 – filmor Jul 12 '12 at 10:27
  • The C++ toolchain parses the file from top to bottom. If it finds a token it doesn't recognise, it dies, with an error message. To avoid this you can forward declare functions, variables, classes, etc. so that it recognises their names. Note, you're not defining them, just declaring them. – Peter Wood Jul 12 '12 at 10:59
  • @filmor ok main is at the bottom of the list. right before void initializeGame() i have added void initializeGame(); void deal(); void playGame(); int dealerDraw(); char generateCard(); void generateDisplay(); and it still gives me the same error. I understand that C++ has to "declare" the functions before it can actually use them (guessing construct them at build time vs runtime?). This however still gave me the error. I have updated my code in question to reflect the change – Matt Westlake Jul 12 '12 at 12:11
  • can you repost the error you're getting now? – Sam I am says Reinstate Monica Jul 12 '12 at 14:20

4 Answers4

2

You can either do a forward declaration before the main function:

void initializeGame();
void deal();
void playGame();

void main (void)
{
  ...
}

void initializeGame()
{
  ...
}

void deal()
{
  ...
}

void playGame()
{
  ...
}

or place the main function definition at the bottom of the file.

Sdra
  • 2,109
  • 14
  • 26
  • Moving the main function to the end of the file won't work as the functions are calling each other. – filmor Jul 12 '12 at 10:27
1

Your updated code seems to be working just fine with me. Here is the screen shot.Screenshot

I cant find what the problem is in your code. It seems you are using Visual Studio. Try cleaning your solution(Build->Clean Solution) and then try rebuilding your code.

Community
  • 1
  • 1
jsist
  • 5,127
  • 3
  • 24
  • 43
  • I still get error C3861: 'initializeGame': identifier not found and a lot of others, but that's the first one – Matt Westlake Jul 12 '12 at 20:20
  • Following are the steps i performed and your code worked. These are as follows: Step 1. Open Visual Studio. Step2. Click on "New project...". Step 3. Choose Visual C++ -> Win32 Console Application and then filled project name and location. Step 4. Remove all the content present in the file that opens and then copy paste the code you have given in above. Step 5. Save the file by pressing Ctrl+S. Step 6. Press F5 to build the solution and run the program. And it worked fine. I can say there seems no problem in concept and in your code. – jsist Jul 13 '12 at 11:24
  • I think there was a problem with my VS install. I uninstalled VS and reinstalled it and it works fine (although the program doesn't totally do what I want, but that's another issue). Seems to build fine now thanks – Matt Westlake Jul 13 '12 at 12:54
0

In general, you need to put identifiers (i.e. function definitions) in the order in which they are called: if you call something, then that something needs to be defined first. Basically turning your program upside down will help you in this case.

Ivo
  • 3,361
  • 1
  • 22
  • 28
  • You don't need to /define/ something before you're calling it. You only need to declare it, that's what's failing here. – filmor Jul 12 '12 at 10:21
0

copy all your functions before main() or In general always declare functions in .h file and define it in .cpp file.

Jeeva
  • 4,329
  • 2
  • 28
  • 55
  • 1
    That is not what header files are for (or, to be precise, how they are used canonically). – filmor Jul 12 '12 at 10:24