c++程序返回数字而不是字符

C++ Program Returning Numbers Instead of Char

本文关键字:字符 数字 程序 返回 c++      更新时间:2023-10-16

我很难弄清楚为什么下面的程序在执行时返回的是数字,而不是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++中的工作方式并正确使用它们之外,没有什么可说的。(或者使用向量或类似的东西)

特别是,getTheaterinitTheater函数不知道数组如何在内存中布局。所以他们不能仅仅用[]来找到元素。

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]);

可以,所以现在,我就这么做。这是给大家的帮助。这无疑给了我一些未来的展望。