Search code examples
c++identifier

Identifier not found error?


I'm writing a code to make a basic text-based Blackjack game in C++. I'm just starting out, but I immediately ran into an issue. Here's the code I have so far, note that it is NOT meant to be complete or even run yet. Yes, I know it is extremely messy and inefficient, I'm in my first C++ class ever.

My issue is that I'm getting "identifier not found" whenever my code calls any recursive functions (DealingHandler and EndProgram). Yes, I know having a function specifically to end the program is completely useless in this program, but I don't know how to suddenly end the program without something like this.

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std; const int maxScore = 21; //immutable upper limit
                                              //string nameInput; - Idea for later.
int HandVal, DealerHandVal, Aceone, val, aceVal;
char HitStay;
string DealerHand, currenthand; //Strings to output the cards ( 4 5 for example)
int numAces, numTwo, numThree, numFour, numFive, numSix, numSeven, numEight, numNine, numTen, numJack, numQueen, numKing; //Inefficient way of making sure it doesn't give five twos out, however unlikely that may be.
int main() {
    cout << "Welcome to our C++ BlackJack Program." << endl;
    cout << "Please press enter to begin the game." << endl;
    system("PAUSE");//Bad.  Still easiest for a beginning C++ class.
    currenthand = "";
    numAces = 4;
    numTwo = 4;
    numThree = 4;
    numFour = 4;
    numFive = 4;
    numSix = 4;
    numSeven = 4;
    numEight = 4;
    numNine = 4;
    numTen = 4;
    numJack = 4;
    numQueen = 4;
    numKing = 4;
    int i = 2;
    {
        DealingHandler(i);
        cout << DealerHand;
        cout << "Press H to hit or S to stay." << endl;
        cin >> HitStay;
        switch (HitStay){
                    case 'H':
                        DealingHandler(1);
                        break;
                    case 'h':
                        DealingHandler(1);
                        break;
                    case 's':
                        EndProgram();
                        break;
                    case 'S':
                        EndProgram();
                        break;
                    default:
                        cout << "Invalid entry.";
                        break;
        }
    }
    system("PAUSE");
}
int DealingHandler(int HowManyDealed) {
    for (int x = 0; x <= HowManyDealed; x++) {
        val = rand() % 13;
        switch (val) {
        case '1':
            cout << "A";
            cout << "Press One to set this Ace equal to one.  Press Two to set it equal to eleven.  This cannot be changed, so choose wisely!" << endl;
            cin >> aceVal;
            if (aceVal = 1) {
                Aceone = 1;
                currenthand += " 1";
                cout << currenthand << endl;
                HandVal += 1;
            }
            if (aceVal = 2) {
                Aceone = 11;
                currenthand += " 11";
                cout << currenthand << endl;
                HandVal += 11;
            }
            numAces = numAces - 1;
        case '2':
            currenthand += " 2";
            cout << currenthand << endl;
            HandVal += 3;
            numTwo -= 1;
            break;
        case '3':
            currenthand += " 3";
            cout << currenthand << endl;
            HandVal += 3;
            numThree -= 1;
            break;
        case '4':
            currenthand += " 4";
            cout << currenthand << endl;
            HandVal += 4;
            numFour -= 1;
            break;
        case '5':
            currenthand += " 5";
            cout << currenthand << endl;
            HandVal += 5;
            numFive -= 1;
            break;
        case '6':
            currenthand += " 6";
            cout << currenthand << endl;
            HandVal += 6;
            numSix -= 1;
            break;
        case '7':
            currenthand += " 7";
            cout << currenthand << endl;
            HandVal += 7;
            numSeven -= 1;
            break;
        case '8':
            currenthand += " 8";
            cout << currenthand << endl;
            HandVal += 8;
            numEight -= 1;
            break;
        case '9':
            currenthand += " 9";
            cout << currenthand << endl;
            HandVal += 9;
            numNine -= 1;
            break;
        case '10':
            currenthand += " 10";
            cout << currenthand << endl;
            HandVal += 10;
            numTen -= 1;
            break;
        case '11':
            currenthand += " J";
            cout << currenthand << endl;
            HandVal += 10;
            numJack -= 1;
            break;
        case '12':
            currenthand += " Q";
            cout << currenthand << endl;
            HandVal += 10;
            numQueen -= 1;
            break;
        case '13':
            currenthand += " K";
            cout << currenthand << endl;
            HandVal += 10;
            numKing -= 1;
            break;
        }
        if (HandVal < 21) {
            cout << "Busted!" << endl;
            EndProgram();
        }

    }
    return HandVal;
}
void EndProgram()
        {
            cout << "I hope you had fun with my game!";
            system("PAUSE");
        }

Thanks in advance for helping me.


Solution

  • This is a great example of declarations, prototypes and definitions, that are something that you need to know.

    Functions prototypes are a way to declare the function structure without his body, for example:

    int my_function(int arg1, int arg2);
    

    In fact, in function prototypes you don't even need to put the arguments name, just their type:

    int my_function(int, int);
    

    As the compilation process goes from top to down, when you have this code

    // function a definition
    int function_a()
    {
        int some_variable = function_b();
    }
    
    // function b definition
    int function_b()
    {
        return 20;
    }
    

    the compiler will know nothing about function_b() when you call her in function_a(), so, you must either reorder your code like this

    // function b definition
    int function_b()
    {
        return 20;
    }
    
    // function a definition
    int function_a()
    {
        // now the compiler knows how function_b() works, or, at least, what arguments she need
        int some_variable = function_b();
    }
    

    or use prototypes to preserve your code organization, like this

    // function b prototype
    int function_b();
    
    // function a definition
    int function_a()
    {
        int some_variable = function_b();
    }
    
    // function b definition
    int function_b()
    {
        return 20;
    }
    

    so, now, the compiler knows that there's a "function_b" that exists, and know what her parameters and return value type are (in this case, there's no parameters and the return type is int)

    For order, you'll usually put the function prototype in the header file (.h) and the definition in the code file (.c/cpp)

    In your example, inside of the main function you are calling the DealingHandler and EndProgram functions, but they're defined later and that's why you get an error. So, using functions prototypes you can fix it easily, putting

    /* this can be
    int DealingHandler(int);
    because function prototypes doesn't need argument's name, just their types */
    int DealingHandler(int HowManyDealed);
    void EndProgram();
    

    upon the main() function, or, better, below this line

    using namespace std;