如何打印 2D 字符数组C++
How to print a 2d character array C++
我做了一个 2D 8x8 数组 ?s.代码编译时只有一个错误"未使用的变量'grid'",这对我来说不是,因为我将网格传递到每个方法中。但是,我没有打印的 8x8 网格 ?s.
// main.cpp
#include <iostream>
#include <iomanip>
using namespace std;
//Declare global variables
const int ROWS = 8; //initialize number of rows
const int COLS = 8; //initialize number of columns
int guesses = 5; //Holds number of palyer's guesses
int main()
{
char grid[ROWS][COLS];
int buildGrid(char grid);
void displayGrid(char grid);
}
int buildGrid (char grid[COLS][ROWS]) {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
grid[col][row] = '?';
}
}
return 0;
}
void displayGrid (char grid[COLS][ROWS]) {
int row, col;
//print multiplication table
for (row = 0; row < ROWS; row++)
{
// prints columns for my row
for (col = 0; col < COLS; col++)
{
cout << setw(3) << grid[row][col];
}
cout << endl;
}
}
看起来你混淆了函数声明和函数调用。 您应该在 main 之前(而不是在 main 中(转发声明函数,然后调用它 main。 这样:
int buildGrid(char grid[COLS][ROWS]);
void displayGrid(char grid[COLS][ROWS]);
int main()
{
char grid[ROWS][COLS];
buildGrid(grid);
displayGrid(grid);
}
相关文章:
- 打印第二列时的2d字符矢量打印空间
- 如何打印 2D 字符数组C++
- 当字符串为"\0"时如何结束 2D 字符数组?
- 如何在 2D 字符数组中打印 [x][y] 元素的地址?(C++)
- 传递动态分配的 2D 字符数组会导致分段错误
- 将字符串与给定的 2d 字符数组匹配
- 打印一行 2D 字符数组 (C++)
- 在C++中打印 2D 字符数组
- 尝试在c++中将文本文件(ifstream)转换为2D字符数组
- 访问动态 2D 字符数组时引发访问冲突异常
- 如何在Visual C 中读取具有字符(无间距)的TXT文件,并将其存储到2D字符阵列中
- C++ 2D 字符数组到字符串
- 使用气泡排序从类中对 2D 字符和 Int 数组进行排序
- 如何在标头中声明后初始化 2D 字符数组
- C++对2D字符数组进行气泡排序
- 声明并分配给 2D 字符指针
- C++将值传递给函数中的2D字符数组
- 包含2D字符数组的构造函数
- 如何从文件中将名称插入2D字符数组
- 输入和输出的2D字符数组问题