在另一个编译器中输出乱码的数组C ++

时间:2019-04-21 14:25:10

标签: c++ c++11 visual-c++ c++14 c++17

我使用Visual Studio软件,并且一切正常。

当数组的输出以乱码出现后,当我使用另一个complire时。

我的代码:

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;
}

Game.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_ */

1 个答案:

答案 0 :(得分:1)

您的问题是print_board[9][9] = {};,类似的问题不会初始化您的2D数组。这是写的越界。

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

与其像这样初始化您的构造函数中的成员变量:

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

{
}