全局字符数组不会被重新分配或正确输出

Global character array won't be reassigned or outputted correctly

我正在尝试制作一个非常基本的井字游戏程序,并将棋盘存储在字符数组中。该数组是全局的,因此我可以从函数输出它,并将 X 和 O 分配给主函数中的位置。我的代码如下。问题是它无法正确编译。如果我在 main 中重新分配它之前添加 char,它会编译但不会 运行 正确。我的C++老师也很困惑,所以我想我会在这里问。 代码:

#include <iostream>
#include "charconvert.h"
#include <ctype.h>
using namespace std;
char board[3][3] =
{
    { ' ', ' ', ' ' },
    { ' ', ' ', ' ' },
    { ' ', ' ', ' ' }
};
void boardout();
int main()
{
    int currentplayer = 1;
    char p1 = 'X';
    char p2 = 'O';
    char pactive;
    int rounds = 1;
    board[3][3] = {
        { '1', '2', '3' },
        { '4', '5', '6' },
        { '7', '8', '9' }
    };
    boardout();
    bool gameover = false;
    do
    {
        if (rounds % 2 == 0){ currentplayer = 1; }
        if (rounds % 2 != 0){ currentplayer = 2; }
        boardout();
        chartoint input;
        char playermove;
        do
        {
            cout << "Player " << currentplayer << ", what space will you go on?" << endl;
            cin >> playermove;
        } while (!isdigit(playermove));
        int space = input.convert(playermove);
        if (currentplayer == 1){ pactive = p1; }
        if (currentplayer == 2){ pactive = p2; }
        if (!(space>9) && !(space<0))
        {
            switch (space)
            {
            case 1:
                if (space == ' ')
                {
                    board[0][0] = pactive;
                }
                else{ cout << "That space is already used. You forfeit this turn." << endl; }
                break;
            case 2:
                if (space == ' ')
                {
                    board[1][0] = pactive;
                }
                else{ cout << "That space is already used. You forfeit this turn." << endl; }
                break;
            case 3:
                if (space == ' ')
                {
                    board[2][0] = pactive;
                }
                else{ cout << "That space is already used. You forfeit this turn." << endl; }
                break;
            case 4:
                if (space == ' ')
                {
                    board[0][1] = pactive;
                }
                else{ cout << "That space is already used. You forfeit this turn." << endl; }
                break;
            case 5:
                if (space == ' ')
                {
                    board[1][1] = pactive;
                }
                else{ cout << "That space is already used. You forfeit this turn." << endl; }
                break;
            case 6:
                if (space == ' ')
                {
                    board[2][1] = pactive;
                }
                else{ cout << "That space is already used. You forfeit this turn." << endl; }
                break;
            case 7:
                if (space == ' ')
                {
                    board[0][2] = pactive;
                }
                else{ cout << "That space is already used. You forfeit this turn." << endl; }
                break;
            case 8:
                if (space == ' ')
                {
                    board[1][2] = pactive;
                }
                else{ cout << "That space is already used. You forfeit this turn." << endl; }
                break;
            case 9:
                if (space == ' ')
                {
                    board[2][2] = pactive;
                }
                else{ cout << "That space is already used. You forfeit this turn." << endl; }
                break;
            default:
                cout << "That is an invalid space. You forfeit your turn." << endl;
            }
        }
        char yn;
        cout << "Is the game over? Enter Y for yes or N for no." << endl;
        cin >> yn;
        if (yn == 'Y' || yn == 'y'){ gameover = true; }
        rounds++;
    } while (!gameover);
    return 0;
}
void boardout()
{
    int x, y;
    x = 0;
    y = 0;
    do
    {
        do
        {
            cout << board[x][y];
            if (x != 2){ cout << " | "; }
            x++;
        } while (x <= 2);
        if (y != 2)
        {
            cout << "\n---------" << endl;
        }
        x = 0;
        y++;
    } while (y <= 2);
}

这里是charconvert.h:

#ifndef CHARCONVERT_H_INCLUDED
#define CHARCONVERT_H_INCLUDED
#include <math.h>
#include <string>
using namespace std;
class chartoint
{
public:
int convert(char);
};

int chartoint::convert(char in)
{
int out = in;
out = out - 48;
return out;
}

class strtonum
{
public:
int strconvert(string);
};

int strtonum::strconvert(string input)
{
int numdigs = input.size();
//cout << input << endl;
//cout << "int numdigs = input.size();\nnumdigs = " << numdigs << endl;
int c=0;
//cout << "int c=0;" << endl;
 int number [numdigs];
//cout << "int number [10] = { };\n";
/*int n=0;
do
{
    cout << "number [" << n << "] = " << number[n] << endl;
    n++;
}
while(n<11);*/
int output = 0;
char tempnumc;
int tempnumi;
//int tenminusc = 10 - c;
if(numdigs<=10)
{
    //cout << "entered if(numdigs<=10)" << endl;
    do
    {
    //cout << "c = " << c << endl;
    tempnumc = input[c];
    //cout << "temp char " << tempnumc << endl;
    tempnumi = tempnumc;
    //cout << "temp i " << tempnumi << endl;
    tempnumi = tempnumi - 48;
    //cout << "temp i " << tempnumi << endl;
    number[c] = tempnumi;
    //cout << "number 10 - c = " << number[tenminusc] << endl;
    //cout << "input[numdigs-c] = number[10-c];" << endl << "numdigs-c = " << numdigs-c << '\t'<< "input[numdigs-c] = " << input[numdigs-c] << '\t' << "number[10-c] = " << number[10-c] << endl;
    //cout << "output = " << output << endl;
    int idontevenknowatthispoint;
    idontevenknowatthispoint = (numdigs-c)-1;
    output = output + (number[c]*pow(10,idontevenknowatthispoint));
    //cout << "output = output + (number[10-c]*pow(10,c));" << endl;
    //cout << "pow(10,c) = " << pow(10,c) << endl;
    //cout << "(number[c]*pow(10,c) = " << number[c]*pow(10,idontevenknowatthispoint) << endl;
    c++;
    tempnumc = ' ';
    tempnumi = 0;
    }
    while(c < numdigs);
    if(numdigs==3)
    {
        output = output + 1;
    }
    if(numdigs==9)
    {
        output = output + 2;
    }
}
else
{
    output = -42;
}
return output;
}

#endif // CHARCONVERT_H_INCLUDED

您不能像这样重新分配数组:

board[3][3] = {
    { '1', '2', '3' },
    { '4', '5', '6' },
    { '7', '8', '9' }
};

由于在 main() 中尝试为其赋值之前从未使用过板,因此只需替换

char board[3][3] =
{
    { ' ', ' ', ' ' },
    { ' ', ' ', ' ' },
    { ' ', ' ', ' ' }
};

char board[3][3] = 
{
    { '1', '2', '3' },
    { '4', '5', '6' },
    { '7', '8', '9' }
};

如果您不想在程序开始时用数字填充全局数组,那么您需要为每个单独的数组元素设置您想要的值。你可以用几个 for 循环来做到这一点:

for (int i = 0, spot = 1; i < 3; i++)
{
    for (int j = 0; j < 3; j++)
    {
        board[i][j] = spot + '0';
        spot++;
    }
}

您声明了 char 数组 "board" 两次 - 一次是全局的(在任何函数之外),另一次是在 main() 中。你只需要声明一次(全局),我会在声明行的开头加上"static"这个词(推荐做法)。

如果您在 main() 中声明 "board",那么您必须将它作为指针传递给较低级别​​的函数(如果您想更改,则使用 '&' 作为指向指针的指针数组)。这是一个网站参考,显示如何分配字符串副本并将其传递给另一个函数(被调用的函数不能更改字符串): https://www.cs.bu.edu/teaching/cpp/string/array-vs-ptr/http://web.cs.swarthmore.edu/~newhall/unixhelp/C_arrays.html

如果您需要更多帮助,请离线联系我。 汤姆·杜兰 tmdurand@gci.net

您不能在主函数中再次重新初始化板数组。您只能在声明时使用初始化程序。它也应该被声明为静态的。如果你想在 main() 中修改它,那么你需要引用各个单元格并给它们一个值。