C ++印刷阵列 [英] C++ Printing Array

查看:124
本文介绍了C ++印刷阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有我使用一个gridmap其中每个阵列块是一个地图上的空间的二维数组。

So I have a 2d array I'm using as a gridmap where each array block is a space on a map.

因此​​,在我的main()我初始化数组'地图'以

So in my main() I've initialized array 'Map' with

char Map[ROWS][COLS]={'x'};

ROWS = 10和COLS = 20

ROWS = 10 and COLS = 20

后来我打电话功能PrintMap以

Later I call function PrintMap with

void PrintMap(const char Map[ROWS][COLS], const bool showTreasure)
{


for (int countr=0; countr<ROWS;countr++){
    for (int countc=0; countc<COLS;countc++){      //print map
            if (!showTreasure){
                    if (Map[countr][countc]!='T')
                            cout << Map[countr][countc];
                    else
                            cout << '*';
            } else {

                    cout << Map[countr][countc];

            }
    }
cout << endl;
}



}

对电网的一个空间,就是在游戏中的宝物或目标的位置。在PrintMap参数是布尔showTreasure这是你是否希望一个T在地图上显示或如果你想隐藏它。

One space on the grid is where the treasure or goal in the game is located. In the parameters for PrintMap is bool showTreasure which is whether or not you want a 'T' to be shown on the map or if you want to hide it.

所以数组中的空间之一将是一个T,而所有其他人将是'*','X',或'P'。

So one of the spaces in the array will be a 'T' while all the others will be either '*', 'X', or 'P'.

*为在这里没有什么是位于

The * is where nothing is located.

因此​​,如果语句都应该说:如果showTreasure是假的(这意味着你要隐藏的T):您打印什么是数组中,如果它不是一个T或打印一个*,如果它是T

So the if statements are supposed to say: If showTreasure is false (meaning you want to hide the 'T'): you print what is in the array if it's not a 'T' or print an '*' if it is a T.

现在我的问题是,即使我已经初始化为200 *在数组,当我运行该程序只打印出1 *。

Now my problem is that even I've initialized the array with 200 *s, when I run the program it only prints out 1 *.

推荐答案

如果您的初始化这一行:

If your initialization is this line:

char Map[ROWS][COLS]={'x'};

则矩阵被初始化为一个 X 其次是199 NULL字符。

Then the matrix is initialized to a single x followed by 199 NULL characters.

参见 http://stackoverflow.com/a/201116/29157

更多关于的std ::填写()请参见的http:// stackoverflow.com/a/3948314/29157

谢谢你的提示, CantChooseUsernames

这篇关于C ++印刷阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆