Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.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++;_C++_Arrays - Fatal编程技术网

C++ 输入及;显示二维字符数组C++;

C++ 输入及;显示二维字符数组C++;,c++,arrays,C++,Arrays,我需要读入一个用户输入文件(这是一个包含所有数字的14x28文本文件),作为二维字符数组。我想我已经用这个代码完成了: getline(cin, fileName); cout << "\n"; infile.open(fileName.c_str()); if (infile.is_open()) for (int i = 0; i < 14; i++) for (int j = 0; j < 28; j++) infile >> m

我需要读入一个用户输入文件(这是一个包含所有数字的14x28文本文件),作为二维字符数组。我想我已经用这个代码完成了:

getline(cin, fileName);
cout << "\n";
infile.open(fileName.c_str());
if (infile.is_open())
for (int i = 0; i < 14; i++)
    for (int j = 0; j < 28; j++)
        infile >> map[i][j];
cout << map[14][28];
getline(cin,文件名);
cout>map[i][j];
如果map被声明为

            char map[14][28];
然后是声明

           cout<< map[14][28];

cout创建另一个循环,类似于您在
map
中阅读的循环:

for (int i = 0; i < 14; i++) {
    for (int j = 0; j < 28; j++) {
        cout << map[i][j] << ","; // You can use a comma or whatever you want, like a space, etc.
    }
    cout << endl; // So each row is broken up, else it will all be on a single long line
}
输出时,可以在打印前检查每个值:

for (int i = 0; i < 14; i++) {
    for (int j = 0; j < 28; j++) {
        if (map[i][j] == 0) cout << " ";
        else if (map[i][j] == 1) cout << "*";
        else if (map[i][j] == 2) cout << "W";
        else if (map[i][j] == 3) cout << "k";
        else cout << (int)map[i][j];
    }
    cout << endl;
}
然后检查
是否映射[i][j]<4
。如果该值为真,您将使用

if (map[i][j] < 4) {
    cout << replacement[ map[i][j] ];
else
    cout << (int)map[i][j];
if(map[i][j]<4){

无法尝试搜索“c++读取文件2d数组”.
不可能复制我不一定认为它应该崩溃。它肯定会调用未定义的行为,其中一个选项是它可能会崩溃。虽然有很多其他的可能性,但……这确实有帮助,我现在可以以14 x 28的方式输出数组,但在最后仍然有一些奇怪的字符底部。给数字分配字母也不太有效,但这个答案给了我更多的工作。如果你用你在输出中看到的“奇怪字符”更新你的原始问题(附加到它,以便人们可以看到“之前”和“之后”),它可能会帮助其他人确定是什么导致这些字符意外打印。
string replacement = " *Wk";
if (map[i][j] < 4) {
    cout << replacement[ map[i][j] ];
else
    cout << (int)map[i][j];