简体   繁体   English

在字符数组C ++中存储整数值

[英]Storing Integer Value in Character Array C++

Hello I am trying to create a tick Tack Toe Game For my College Project, The Board size of the game needs to be GENERIC using 2D array in C++. 您好,我正在尝试为我的大学项目创建一个Tick Toe Toe游戏,该游戏的棋盘大小需要使用C ++中的2D数组进行通用设置。 So I'm having trouble while initializing Default numbers(Places) identifier in an array 所以我在初始化数组中的默认数字(地方)标识符时遇到麻烦

    for (int i = 0; i < SIZE; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            Boards[i][j] = initial++;
        }
    }
    for (int i = 0; i < SIZE; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            if (Boards[i][j] < 10) cout << " " << Boards[i][j] << "   |   ";
            else cout << Boards[i][j] << "   |   ";
        }

        cout << endl;
    }

As the variable 'initial' is a integer and i have to increment it in loop.I am quite not sure how to save it in char array (BOARD) Board needs to be char to display X,O 由于变量'initial'是一个整数,我必须在循环中对其进行递增。我不确定如何将其保存在char数组(BOARD)中Board需要为char才能显示X,O

Now that you have posted the full code, I can see a problem on this line and the other one like it: 现在,您已经发布了完整的代码,我可以在此行上看​​到一个问题,而另一行是这样的:

cout << Boards[i][j] << "   |   ";

Since the type of Boards[i][j] is a char , the C++ standard library will just send that char to your terminal, and the terminal will try to interpret it as an ASCII character. 由于Boards[i][j]类型为char ,因此C ++标准库只会将该char发送到您的终端,终端将尝试将其解释为ASCII字符。 You need to cast it to an int first so that the C++ standard library will format it properly for you: 您需要先将其转换为int以便C ++标准库为您正确格式化它:

cout << (int)Boards[i][j] << "   |   ";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM