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

时间:2015-06-03 16:33:27

标签: c++ arrays char global

我试图制作一个非常基本的tic tac toe程序,并且我将电路板存储在字符数组中。该数组是全局的,因此我可以从函数输出它,并将X和O分配给main函数中的位置。我的代码如下。问题是它不能正确编译。如果我在将它重新分配给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

3 个答案:

答案 0 :(得分:1)

你声明了char数组&#34; board&#34;两次 - 一次全局(在任何函数之外),再次在main()中。你只需要声明一次(全局),然后我会添加单词&#34; static&#34;在申报行的开头(推荐做法)。

如果你声明&#34; board&#34;在main()中,你必须将它作为指针传递给低级函数(如果要更改数组,则使用&#39;&amp;&#39;作为指针的指针)。这是一个网站参考,显示如何分配和传递字符串的副本到另一个函数(被调用的函数不能更改字符串): https://www.cs.bu.edu/teaching/cpp/string/array-vs-ptr/http://web.cs.swarthmore.edu/~newhall/unixhelp/C_arrays.html

如果您需要更多帮助,请离线查看。 汤姆杜兰德 tmdurand@gci.net

答案 1 :(得分:0)

您无法重新分配数组:

 "{% url 'get_ticker' %}"

由于您在board[3][3] = { { '1', '2', '3' }, { '4', '5', '6' }, { '7', '8', '9' } }; 只是替换

之前尝试为其分配值之前从未使用过电路板
main()

使用

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

如果您不希望在程序开头填充全局数组,那么您需要使用所需的值设置每个单独的数组元素。你可以用一对for循环来做到这一点:

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

答案 2 :(得分:0)

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