Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我的阵列没有更新?_C++ - Fatal编程技术网

C++ 为什么我的阵列没有更新?

C++ 为什么我的阵列没有更新?,c++,C++,所以我尝试制作一个地下城爬行游戏(基本上是一个10x10的地图),当我创建地图(棋盘)并更改一个元素时,它不会在更新时打印出来。我没有发现我的代码有任何问题,我没有其他地方可以求助:( #包括 #包括“ChadDung.h” 使用名称空间std; int createBoard(); int updateBoard(); int clear(); 炭板[10][10]; int xp=10; int-yp=4; int main(){ createBoard(); 板[xp][yp]=“G”;

所以我尝试制作一个地下城爬行游戏(基本上是一个10x10的地图),当我创建地图(棋盘)并更改一个元素时,它不会在更新时打印出来。我没有发现我的代码有任何问题,我没有其他地方可以求助:(

#包括
#包括“ChadDung.h”
使用名称空间std;
int createBoard();
int updateBoard();
int clear();
炭板[10][10];
int xp=10;
int-yp=4;
int main(){
createBoard();
板[xp][yp]=“G”;
清除();
updateBoard();
}
int createBoard(){
对于(int x=0;x<10;x++){
对于(int y=0;y<10;y++){
板[x][y]=';

cout
字符板[10][10];

xp=10;

board[xp][yp]='G';


当您设置“G”时,您已经跑过了数组的末尾(有效索引为0-9),因此您得到了未定义的行为。

您在数组中的错误位置添加了“G”。您超出了限制。您的数组从[0][0]到[9][9]不等。

首先,我尝试了您的代码,但出现了错误,因为您的函数没有返回值(您将它们声明为返回int)因此,更改这些值以返回void。然后,为了回答您的问题,您没有看到“G”的原因是您的xp值为10,这超出了网格的范围。请记住,在代码中,通常情况下,事情是以0为基础的,因此插槽1-10实际上被称为插槽0-9。因此,我将sp=10更改为xp=9,您的“G”显示为魅力


#包括
void createBoard();
void updateBoard();
无效清除();
使用名称空间std;
void createBoard();
void updateBoard();
无效清除();
炭板[10][10];
int xp=9;
int-yp=4;
int main(){
createBoard();
板[xp][yp]=“G”;
清除();
updateBoard();
}
void createBoard(){
对于(int x=0;x<10;x++){
对于(int y=0;y<10;y++){
板[x][y]=';

cout索引从0开始。board[xp][yp]='G';不是有效的Hanks!现在我觉得自己很愚蠢,因为我记得学过这个:P
#include <iostream>
#include "ChadDung.h"
using namespace std;

int createBoard();
int updateBoard();
int clear();
char board[10][10];
int xp = 10;
int yp = 4;

int main() {

    createBoard();
    board[xp][yp] = 'G';
    clear();
    updateBoard();

}

int createBoard(){
    for (int x = 0; x < 10; x++){

        for (int y = 0; y < 10; y++){
            board[x][y] = '.';
            cout << board[x][y];
        }
        cout << endl;
    }
}


int updateBoard(){
    for (int x = 0; x < 10; x++){

        for (int y = 0; y < 10; y++){
            cout << board[x][y];
        }
        cout << endl;
    }
}


int clear(){
    cout << string( 25, '\n' );
}
#include <iostream>

void createBoard();
void updateBoard();
void clear();

using namespace std;

void createBoard();
void updateBoard();
void clear();
char board[10][10];
int xp = 9;
int yp = 4;

int main() {

    createBoard();
    board[xp][yp] = 'G';
    clear();
    updateBoard();

}

void createBoard() {
    for (int x = 0; x < 10; x++){

        for (int y = 0; y < 10; y++){
            board[x][y] = '.';
            cout << board[x][y];
        }
        cout << endl;
    }
}


void updateBoard(){
    for (int x = 0; x < 10; x++){

        for (int y = 0; y < 10; y++){
            cout << board[x][y];
        }
        cout << endl;
    }
}


void clear(){
    cout << string( 25, '\n' );
}