我制作了2D 8x8数组的?代码只编译了一个错误“未使用的变量'grid'”,这对我来说是没有的,因为我正在将网格传递给每种方法。但是,我没有打印的8x8的?。
// 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;
}
}
答案 0 :(得分:3)
看起来您在混淆函数声明和函数调用。您应该在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);
}