我无法弄清楚为什么以下程序在执行时返回数字而不是5行和10列磅符号。它似乎正在返回一个内存地址,但我不确定。基本上是随地吐痰" 52428"而不是英镑符号,但正确的模式为5乘10。 基本上我看到了这个:
52428524285242852428524285242852428524285242852428
52428524285242852428524285242852428524285242852428
52428524285242852428524285242852428524285242852428
52428524285242852428524285242852428524285242852428
52428524285242852428524285242852428524285242852428
以下代码:
#include <iostream>
using namespace std;
//Constants for Total Rows and Total Columns
static const unsigned short TOT_ROWS = 5, TOT_COLUMNS = 10;
//Function Prototypes
unsigned short convertChar(char);
bool isActive(unsigned short);
void initTheater(char[]);
void getTheater(char[]);
void updateheater(char[], unsigned short, unsigned short);
void storeTheater(char[]);
int main()
{
//Variable and Array Decs
char theater[TOT_ROWS][TOT_COLUMNS];
double price[TOT_ROWS];
char selection;
//Get price input per row
for (unsigned short rowNum = 0; rowNum < TOT_ROWS; rowNum++)
{
cout << "Enter the price for row " << rowNum+1 << ":";
cin >> price[rowNum];
}
//Initialize Theater
initTheater(*theater);
//Loop to wait for one of the exit commands
do
{
getTheater(*theater);
cout << "Enter a selection: ";
cin >> selection;
} while (isActive(selection));
return 0;
}
//Initalize theater by placing '#' in each array element
void initTheater(char theater[])
{
for (unsigned short rows = 0; rows < TOT_ROWS; rows++)
{
for (unsigned short cols = 0; cols < TOT_COLUMNS; cols++)
{
theater[rows][&cols] = '#';
}
}
}
//Display current state of theater
void getTheater(char *theater)
{
for (unsigned short viewRows = 0; viewRows < TOT_ROWS; viewRows++)
{
for (unsigned short viewCols = 0; viewCols < TOT_COLUMNS; viewCols++)
{
cout << theater[viewRows][&viewCols];
}
cout << endl;
}
}
//Update the Theater by placing a '*' or '#' in the specific row and seat.
void updateTheater(char *theater[], unsigned short row, unsigned short column)
{
//Expand to determine current state of array element and flip to the alternate
theater[row][column] = '*';
}
//Check if user has typed exit command and exit if yes
bool isActive(unsigned short selection)
{
if (selection == '9' || selection == 'q' || selection == 'Q')
{
return false;
}
return true;
}
答案 0 :(得分:3)
阵列根本不起作用,就像你似乎期待它们一样。除了你需要了解数组如何在C ++中工作并适当地使用它们之外,没什么可说的。 (或使用矢量或类似的东西。)
特别是,getTheater
和initTheater
函数不知道数组如何在内存中布局。所以他们只能使用[]
来找到元素。
在getTheater
中,你有这个:
cout << theater[viewRows][&viewCols];
在C和C ++中,a[b][c]
相当于*(*(a+b)+c)
。所以上面相当于:
cout << *(*(theater + viewRows) + &viewCols);
重新排列:
cout << * (&viewCols + *(theater+viewRows));
这与:
相同char j = theater[viewRows];
cout << * (&viewCols + j);
所以你看看内存中的内容有点过viewCols
,查看数组的错误元素,以确定过去viewCols
的距离。
答案 1 :(得分:0)
我刚刚引用了我正在学习的书,并意识到他们在函数声明中明确声明了数组的第二维:
//Constants for Total Rows and Total Columns
static const unsigned short TOT_ROWS = 5, TOT_COLUMNS = 10;
//Function Prototypes
unsigned short convertChar(char);
bool isActive(unsigned short);
void initTheater(char[][TOT_COLUMNS]);
void getTheater(char[][TOT_COLUMNS]);
void updateTheater(char[][TOT_COLUMNS], unsigned short, unsigned short);
void storeTheater(char[][TOT_COLUMNS]);
这是有效的,所以现在,我会继续这样做。那是每个人的帮助。绝对给我一些其他东西来看待未来。