C++ Program Returning Numbers Instead of Char -
i'm having trouble figuring out why below program returning numbers when executed instead of 5 rows , 10 columns of pound symbols. seems it's returning memory address, i'm not sure. spitting out "52428" instead of pound symbol in correct pattern of 5 10. see this:
52428524285242852428524285242852428524285242852428 52428524285242852428524285242852428524285242852428 52428524285242852428524285242852428524285242852428 52428524285242852428524285242852428524285242852428 52428524285242852428524285242852428524285242852428
code below:
#include <iostream> using namespace std; //constants total rows , 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 , array decs char theater[tot_rows][tot_columns]; double price[tot_rows]; char selection; //get price input per row (unsigned short rownum = 0; rownum < tot_rows; rownum++) { cout << "enter price row " << rownum+1 << ":"; cin >> price[rownum]; } //initialize theater inittheater(*theater); //loop wait 1 of exit commands { gettheater(*theater); cout << "enter selection: "; cin >> selection; } while (isactive(selection)); return 0; } //initalize theater placing '#' in each array element void inittheater(char theater[]) { (unsigned short rows = 0; rows < tot_rows; rows++) { (unsigned short cols = 0; cols < tot_columns; cols++) { theater[rows][&cols] = '#'; } } } //display current state of theater void gettheater(char *theater) { (unsigned short viewrows = 0; viewrows < tot_rows; viewrows++) { (unsigned short viewcols = 0; viewcols < tot_columns; viewcols++) { cout << theater[viewrows][&viewcols]; } cout << endl; } } //update theater placing '*' or '#' in specific row , seat. void updatetheater(char *theater[], unsigned short row, unsigned short column) { //expand determine current state of array element , flip alternate theater[row][column] = '*'; } //check if user has typed exit command , exit if yes bool isactive(unsigned short selection) { if (selection == '9' || selection == 'q' || selection == 'q') { return false; } return true; }
arrays don't work @ seem expecting them work. there's not other need learn how arrays work in c++ , use them appropriately. (or use vectors or that.)
in particular, gettheater
, inittheater
function have no idea how array laid out in memory. can't find elements using []
's.
in gettheater
, have this:
cout << theater[viewrows][&viewcols];
in c , c++, a[b][c]
equivalent *(*(a+b)+c)
. above equivalent to:
cout << *(*(theater + viewrows) + &viewcols);
rearranging:
cout << * (&viewcols + *(theater+viewrows));
this same as:
char j = theater[viewrows]; cout << * (&viewcols + j);
so you're looking @ what's in memory bit past viewcols
, looking @ wrong element of array decide how far past viewcols
look.
Comments
Post a Comment