c++程序返回数字而不是字符
C++ Program Returning Numbers Instead of Char
我很难弄清楚为什么下面的程序在执行时返回的是数字,而不是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;
}
数组根本不像你期望的那样工作。除了需要了解数组在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
的距离。
我只是参考了我正在学习的那本书,并意识到他们在函数声明中显式地声明了数组的第二次维度,如下所示:
//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]);
可以,所以现在,我就这么做。这是给大家的帮助。这无疑给了我一些未来的展望。
相关文章:
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何检查一个c++字符串中有多少相同的字符/数字
- 当我输入字符类型的数字时,为什么我无法获得整数?
- scanf() 只取数字作为输入,避免使用其他字符
- 递归形成字符串中所有数字字符的中间和?
- 首先按给定顺序打印所有数字,然后使用 Array 打印所有字符和其他符号
- 如何在 getline 中删除/忽略超过特定数字的字符?
- C++输出奇怪的字符而不是数字 (Windows)
- 为什么strlen(s)与s的大小不同,为什么cout-char显示的是字符而不是数字
- 如何在C++中有效地将数字值重新分配给字符数组
- 我应该以字符形式存储数字以节省内存吗?
- C++ 从 std::string 中删除所有非数字字符
- 在c++中读取文件时,它如何自动将字符放入字符数组,将数字放入整数变量
- 不反转数字的字符反转
- 如何检查字符串是否包含所有这些:数字、字母和特殊字符
- 如果是数字或字符,请检查输入
- 如何将字符更改为数字?
- 生成随机字符/数字
- 如何将 std::string 中的所有非字母字符(数字和特殊字符)替换为空格
- 字符数字 ['0'.."9'] 需要具有连续的数值吗?