首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在另一个编译器中打印乱码的数组| C++

在另一个编译器中打印乱码的数组| C++
EN

Stack Overflow用户
提问于 2019-04-21 22:25:10
回答 1查看 122关注 0票数 -1

我使用Visual Studio软件,一切工作正常。

当我在数组的输出以乱码的形式输出之后,我使用了另一个编译器。

我的代码:

print_board[9][9] = {};
print_board[0][0] = '\0';

然后我将数据输入到数组中,它们会像这样打印:

?????·?²?
?|?-`???
???-?????
??|Z%??
?????????
·1²??? 
?????????
?????] @?
?????????

这是打印的代码:

void Game::PrintWin(char print_board[][9]) {
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            cout << print_board[i][j];
        }
        cout << endl;
    }
}

这是我得到的唯一错误,但我不知道它是否与我的问题有关:

Game.cpp:5:23: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
  print_board[9][9] = {};

有没有人知道为什么会这样?

我不喜欢写太多代码,因为它会加载到论坛上,但下面是代码:

Game.cpp:

#include "Game.h"

Game::Game() {
    board[5][5] = {};
    print_board[9][9] = {};
    tmp_input[5] = {};
    example = "PASS";
    quit = "QUIT";
    play2 = 0;

    tmp_input[0] = '\0';
    print_board[0][0] = '\0';
    board[5][5] = '\0';

}

void Game::Start() {
    TableReady(print_board);
    PlayV();
    return;
}

int Game::getPlay2() {
    return play2;
}
void Game::setPlay2(int num) {
    play2 = num;
}

void Game::TableReady(char print_board[][9]) {
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            if ((i % 2 == 0) & (j % 2 == 0))
                print_board[i][j] = 'O';
        }
    }
}

bool Game::Shave(char arr[]) {
    for (int i = 0; i < 5; i++) {
        if (arr[i] != example[i])
            return false;
    }
    return true;
}

bool Game::CheckIfNumbers(char arr[], int board[][5]) {
    for (int i = 0; i < 5; i++) {
        if (i == 2)
            continue;
        if ((arr[i] - 48 > 5) | (arr[i] - 48 < 1) | (arr[2] != ' ')) {
            cout << "Invalid move; the game awaits a valid move." << endl;
            return false;
        }
    }
    string array1(arr);
    string array2(arr);
    string num1 = array1.substr(0, 2);
    string num2 = array2.substr(3, 5);
    int a = atoi(num1.c_str());
    int b = atoi(num2.c_str());
    if ((abs(a - b) != 10) & (abs(a - b) != 1)) {
        cout << "Invalid move; the game awaits a valid move." << endl;
        return false;
    }

    string array_a(arr);
    string num_a = array_a.substr(0, 1);
    string num_b = array_a.substr(1, 2);
    string num_c = array_a.substr(3, 3);
    string num_cAgain = num_c.substr(0, 1);
    string num_d = array_a.substr(4, 5);
    int a_1 = atoi(num_a.c_str());
    int b_2 = atoi(num_b.c_str());
    int c_3 = atoi(num_cAgain.c_str());
    int d_4 = atoi(num_d.c_str());
    if ((board[b_2 - 1][a_1 - 1] == 2) | (board[d_4 - 1][c_3 - 1] == 2)) {
        cout << "Invalid move; the game awaits a valid move." << endl;
        return false;
    }
    return true;
}

bool Game::CheckIfQuit(char arr[]) {
    for (int i = 0; i < 5; i++) {
        if (arr[i] != quit[i])
            return false;
    }
    return true;
}

void Game::InsertPlay(char arr[], int board[][5], char print_board[][9]) {
    string array(arr);
    string num1 = array.substr(0, 1);
    string num2 = array.substr(1, 2);
    string num3 = array.substr(3, 3);
    string num3Again = num3.substr(0, 1);
    string num4 = array.substr(4, 5);
    int a = atoi(num1.c_str());
    int b = atoi(num2.c_str());
    int c = atoi(num3Again.c_str());
    int d = atoi(num4.c_str());
    board[b - 1][a - 1]++;
    board[d - 1][c - 1]++;
    if (b == d) {
        int minLine = 0;
        if (a < c)
            minLine = a;
        else
            minLine = c;
        if (b == 1)
            print_board[0][minLine * 2 - 1] = '-';
        else if (b == 2)
            print_board[2][minLine * 2 - 1] = '-';
        else if (b == 3)
            print_board[4][minLine * 2 - 1] = '-';
        else if (b == 4)
            print_board[6][minLine * 2 - 1] = '-';
        else if (b == 5)
            print_board[8][minLine * 2 - 1] = '-';
    }
    else {
        int minColumn = 0;
        if (b < d)
            minColumn = b;
        else
            minColumn = d;
        if (a == 1)
            print_board[minColumn * 2 - 1][0] = '|';
        else if (a == 2)
            print_board[minColumn * 2 - 1][2] = '|';
        else if (a == 3)
            print_board[minColumn * 2 - 1][4] = '|';
        else if (a == 4)
            print_board[minColumn * 2 - 1][6] = '|';
        else if (a == 5)
            print_board[minColumn * 2 - 1][8] = '|';
    }
}

void Game::PrintWin(char print_board[][9]) {
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            cout << print_board[i][j];
        }
        cout << endl;
    }
}

bool Game::CheckIfWin(char print_board[][9]) {
    for (int i = 0; i < 9; i++) {
        if ((print_board[i][1] == '-') & (print_board[i][3] == '-') & (print_board[i][5] == '-') & (print_board[i][7] == '-')) {
            cout << "H wins the game." << endl;
            return true;
        }
        i++;
    }
    for (int i = 0; i < 9; i++) {
        if ((print_board[1][i] == '|') & (print_board[3][i] == '|') & (print_board[5][i] == '|') & (print_board[7][i] == '|')) {
            cout << "V wins the game." << endl;
            return true;
        }
        i++;
    }
    return false;
}

bool Game::CheckIfPass(char player, char arr[]) {
    if (!Shave(arr))
        return false;
    return true;
}

bool Game::CheckIfDoublePlay(char player) {
    if (play2 == 1) {
        cout << "The game ends in a tie." << endl;
        return false;
    }
    else {
        play2 = play2 + 1;
        return true;
    }
}

void Game::PlayV() {
    cout << "V:" << endl;
    cin.getline(tmp_input, 6);
    if (CheckIfPass('V', tmp_input)) {
        if (CheckIfDoublePlay('V')) {
            PlayH();
            return;
        }
        else {
            PrintWin(print_board);
            return;
        }
    }
    if (CheckIfQuit(tmp_input)) {
        cout << "H wins the game." << endl;
        PrintWin(print_board);
        return;
    }
    if (!CheckIfNumbers(tmp_input, board)) {
        play2 = 0;
        PlayV();
        return;
    }
    play2 = 0;
    InsertPlay(tmp_input, board, print_board);
    if (CheckIfWin(print_board)) {
        PrintWin(print_board);
        return;
    }
    PlayH();
    return;
}

void Game::PlayH() {
    cout << "H:" << endl;
    cin.getline(tmp_input, 6);

    if (CheckIfPass('H', tmp_input)) {
        if (CheckIfDoublePlay('H')) {
            PlayV();
            return;
        }
        else {
            PrintWin(print_board);
            return;
        }
    }

    if (CheckIfQuit(tmp_input)) {
        cout << "V wins the game." << endl;
        PrintWin(print_board);
        return;
    }
    if (!CheckIfNumbers(tmp_input, board)) {
        play2 = 0;
        PlayH();
        return;
    }
    play2 = 0;
    InsertPlay(tmp_input, board, print_board);
    if (CheckIfWin(print_board)) {
        PrintWin(print_board);
        return;
    }
    PlayV();
    return;
}

游戏.h:

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

#ifndef GAME_H_
#define GAME_H_

class Game {
private:
    int board[5][5];
    char print_board[9][9];
    char tmp_input[5];
    const char* example;
    const char* quit;
    int play2;
public:
    Game();
    void Start();
    void PlayV();
    void PlayH();
    bool CheckIfPass(char player, char arr[]);
    bool Shave(char arr[]);
    bool CheckIfDoublePlay(char player);
    bool CheckIfNumbers(char arr[], int board[][5]);
    bool CheckIfQuit(char arr[]);
    void InsertPlay(char arr[], int board[][5], char print_board[][9]);
    bool CheckIfWin(char print_board[][9]);
    void PrintWin(char print_board[][9]);
    void TableReady(char print_board[][9]);
    int getPlay2();
    void setPlay2(int num);
};

#endif /* GAME_H_ */
EN

回答 1

Stack Overflow用户

发布于 2019-04-22 00:49:46

你的问题是print_board[9][9] = {};和类似的,不要初始化你的2D数组。这是一个越界写入。

您的数组的有效索引是print_board[0][0]print_board[8][8],您的代码正在尝试访问超出数组末尾的print_board[9][9]。这也是一个单一的元素,而不是整个数组。

而不是像这样在构造函数中初始化成员变量:

Game::Game() : board{}, print_board{}, tmp_input{}, example{"PASS"}, quit{"QUIT"},play{}

{
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55783586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档