Question

I am a beginner in programming and C++.

Why my variables are not declared? I declare them with Initialize() func, and this function is before Update().

Are these variables not global?

#include <iostream>

using namespace std;


void Initialize()
{
    string curPlayerName = "PlayerX";

    char squareText[9] = {'x', 'a'};

    int input;
}

void Update()
{
    cout << "\n\n \t\t" << "TIC TAC TOE" << endl;

    cout << "\n \t" << "     |     |     ";
    cout << "\n \t" << "  " >> squareText[0] >> "  |  1  |  2  ";
    cout << "\n \t" << "_____|_____|_____";
    cout << "\n \t" << "     |     |     ";
    cout << "\n \t" << "  3  |  4  |  5  ";
    cout << "\n \t" << "_____|_____|_____";
    cout << "\n \t" << "     |     |     ";
    cout << "\n \t" << "  6  |  7  |  8  ";
    cout << "\n \t" << "     |     |     ";

    cout << "\n\n \t" << curPlayerName << ", enter a number:" << endl;
}

int main()
{
    Initialize();
    Update();
    /*cin >> input;

    switch(input)
    {
    case 0:
        break;
    }*/


    cout << "\n\n\n";
    return 0;
}

Here are the errors:

In function 'void Update()' :
   error: 'squareText' was not declared in this scope
   error: 'curPlayerName' was not declared in this scope
Était-ce utile?

La solution

You have to declare both variables in global scope, that's to say before

void Initialize()

This way all your functions will be able to access these variables.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top