未在范围内声明的商标游戏变量

问题描述 投票:-1回答:2

你好,我是C ++的新手,我想构建一个tictactoe游戏,但是当我编译该游戏时,总是会出现以下错误:我的变量未在范围内声明。然后,我尝试将平方作为全局变量,但这给了我很多错误。

有人可以帮我吗?

include <iostream>

using namespace std;
void Game();
void Spielerfeld();

void Game(){
        int player;
        int choice;
        char mark;

        Spielerfeld();
        do{

            cout << "Wer ist als erstes in der Reihe\n" << choice;
            player = 1;
            cout << "Player:" << player << "Gebe eine nummer ein";
            cin >> choice;
            mark = (player);'X';'O';
            if(choice == 1 && square[1] == '1')
                square[1] == mark;
            else if(choice == 2 && square[2] == '2')
                square[2] = mark;
            else if(choice == 3 && square[3] == "2")
                square[3] = mark;
            else if(choice == 4 && square[4] == '4')
                sqaure[4] = mark;
            else if(choice == 5 && square[5] ==  "5")
                sqaure[5] == mark;
            else if(choice == 6 && square[6] ==  "6")
                sqaure[6] == mark;
            else if(choice == 7 && square[7] ==  "7")
                sqaure[7] == mark;
            else if(choice == 8 && square[8] ==  "8")
                sqaure[8] == mark;
            else if(choice == 9 && square[9] ==  "9")
                sqaure[9] == mark;
            }while(choice == 9);
}


 void Spielerfeld(){
    int square[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int i;
    for(i=0; i<10; i++){
        cout << square[i] << endl;
    }

    cout << square[1] << "|" << square[2] << "|" << sqaure[3];
    cout << "-----------|--------";
    cout << square[4] << "|" << square[5] << "|" << sqaure[6];
    cout << "-----------|--------";
    cout << square[7] << "|" << square[8] << "|" << sqaure[9];
 }

int main(){
    Spielerfeld();``
    Game();
}
c++ tic-tac-toe
2个回答
2
投票

1] Square是Spielerfeld()函数中的局部变量,在函数结束后会销毁,因此您无法使用它。

2)您将“平方”定义为内置数组,并且内置数组元素以零索引开头。因此,当您要访问第一个元素时,应使用Square [0]。

我将Square定义为全局变量,然后修改您的代码。现在尝试一下。


#include <iostream>

using namespace std;
void Game();
void Spielerfeld();
int square[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

void Game() 
{
    int player = 0;
    int choice = 0;
    char mark = 0;

    Spielerfeld();
    do {

        cout << "Wer ist als erstes in der Reihe\n" << choice;
        player = 1;
        cout << "Player:" << player << "Gebe eine nummer ein";
        cin >> choice;
        mark = (player); 'X'; 'O';
        if (choice == 1 && square[0] == 1)
            square[0] = mark;
        else if (choice == 2 && square[1] == 2)
            square[1] = mark;
        else if (choice == 3 && square[2] == 2)
            square[2] = mark;
        else if (choice == 4 && square[3] == 4)
            square[3] = mark;
        else if (choice == 5 && square[4] == 5)
            square[4] = mark;
        else if (choice == 6 && square[5] == 6)
            square[5] = mark;
        else if (choice == 7 && square[6] == 7)
            square[6] = mark;
        else if (choice == 8 && square[7] == 8)
            square[7] = mark;
        else if (choice == 9 && square[8] == 9)
            square[8] = mark;
    } while (choice == 9);
}


void Spielerfeld() {

    int i;

    for (i = 0; i < 9; i++) 
    {
        cout << square[i] << endl;
    }

    cout << square[0] << "|" << square[1] << "|" << square[2];
    cout << "-----------|--------";
    cout << square[3] << "|" << square[4] << "|" << square[5];
    cout << "-----------|--------";
    cout << square[6] << "|" << square[7] << "|" << square[8];

}

int main() 
{
    Spielerfeld();
    Game();
}

0
投票

您有一个简单的问题。在C ++中,==运算符和=运算符之间存在差异。简而言之,==用于等式comparison=用于变量分配。所有说square[i] == mark;的行都应该说square[i] = mark;

© www.soinside.com 2019 - 2024. All rights reserved.