简体   繁体   English

2D Char Array中出现奇怪的字符

[英]Strange characters appearing in 2D Char Array

I'm coding a game that utilizes a 'grid', which I have created using a 2 dimensional array of structs, which contain a char value and a boolean value. 我正在编写一个利用'网格'的游戏,我使用2维结构数组创建,其中包含char值和布尔值。 In my program's .h file, I declare the struct and create the grid. 在我的程序的.h文件中,我声明了struct并创建了网格。

struct Tile
{
    char letter;
    bool active;
};

Tile grid [6][5];

In my .cpp file, I initialize the grid so that all values are blank. 在我的.cpp文件中,我初始化网格,以便所有值都为空。

 for (int i = 0; i < 7; ++i)
    {
        for (int j = 0; j < 6; ++j)
        {
            grid[i][j].active == false;
            //grid[i][j].letter = '.';
            //it always crashes when i try doing the above line
        }

    }

The function that prints the grid, printGrid, is below 打印网格printGrid的功能如下所示

for (int i = 0; i < 7; ++i)
    {
        for (int j = 0; j < 6; ++j)
        {
            cout << i;
            //the above statement is for debugging purposes so that I can see
            //which column easier
            std::cout << grid[i][j].letter;
        }
        std::cout << std::endl;
    }
    cout << "1 2 3 4 5 6" << endl;

Now, the original goal was to have the default .letter value be '.'. 现在,最初的目标是将默认.letter值设置为'。'。 But for some reason, when I tried to do this, there are disastrous results; 但出于某种原因,当我试图这样做时,会有灾难性的后果; the screen fills up with characters moving so fast I can't entirely see what they are (I recall some hearts and smiley faces), along with an obnoxious, rapid beeping. 屏幕填满了人物移动如此之快我无法完全看到它们是什么(我记得一些心和笑脸),以及令人讨厌,快速的哔哔声。 So I decided to leave that commented line out. 所以我决定将该评论专线留下。

When I run the program without that line, for some reason, the "grid" always displays characters in certain spots, without any input from the user, or without me having expressly declared any values to that spot. 当我在没有该行的情况下运行程序时,由于某种原因,“网格”总是在某些位置显示字符,没有来自用户的任何输入,或者没有我明确地声明该位置的任何值。 For instance, the spot of the 1st column from the left and the bottom row, always has a character in it (grid[6][5].letter). 例如,左边和底行的第一列的点总是有一个字符(grid [6] [5] .letter)。 It changes every time I run the program, and I've seen it range from a heart, to the letter A, to the spanish 'n' (the one with a ~ over it). 每次我运行程序时它都会改变,我看到它的范围从一个心脏,一个字母A到西班牙语'n'(一个带有〜的那个)。

I thought to myself, "Hey, since grid[6][5] is the spots that are always buggy, I'll just declare those individual spot's .letter values to be blank (' ')!". 我心想,“嘿,因为网格[6] [5]是总是有虫子的地方,我只会声明那些个别地点的.letter值是空白的('')!”。 That didn't work. 那没用。

I've got no idea why this one spot is giving me trouble. 我不知道为什么这一个地方给我带来了麻烦。 There were other areas that would have an abnormal character, but I was able to neutralize them by setting their .letter values to blank. 还有其他区域会出现异常,但我可以通过将其.letter值设置为空白来中和它们。 If anyone has any idea on how to fix this, pleas 如果有人知道如何解决这个问题,请求

EDIT: The other abnormal characters, which appear at grid[6][0], grid[6][1], grid[6][5], and grid[6][4], all make my program crash at later stages if I set them to blank (' '); 编辑:出现在网格[6] [0],网格[6] [1],网格[6] [5]和网格[6] [4]中的其他异常字符都会使我的程序在以后崩溃如果我将它们设置为空白(''); however, blanking grid[6][5] is the one that makes it crash at the get go. 然而,消隐网格[6] [5]是一个让它在开始时崩溃的网格。 I tried using a debugger, but it wasn't able to tell me anything helpful. 我尝试使用调试器,但它无法告诉我任何有用的信息。

you're running over the end of your arrays 你正在运行数组的末尾

Tile grid [6][5]; needs to be Tile grid [7][6]; 需要是Tile grid [7][6];

or you need to loop only to i < 6 and j < 5 . 或者你只需​​要循环到i < 6j < 5

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

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