Sign in to follow this  

Problem with displaying text (C++)

This topic is 3153 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When I run this program it displays a bunch of random characters instead of all +'s. The source is below. #include <iostream> using namespace std; class object{ protected: int x, y; char graphic; public: const int get_x(){return x;}; const int get_y(){return y;}; const char get_graphic(){return graphic;}; void set_x(int new_x){x = new_x;}; void set_y(int new_y){y = new_y;}; void set_graphic(int new_graphic){graphic = new_graphic;}; }; /* There is some bug in here class agent:protected object{ protected: int x, y; char graphic; public: const int get_x(){return x;}; const int get_y(){return y;}; const char get_graphic(){return graphic;}; void set_x(int new_x){x = new_x;}; void set_y(int new_y){y = new_y;}; void set_graphic(int new_graphic){graphic = new_graphic;}; }; */ // ++ create a tile class that allows multiple objects in one tile /* Theoretically I would create an object array or vector and a setting function that looks for an empty cell to jump into */ class tile{ protected: int x, y; object obj; public: const int get_x(){return x;}; const int get_y(){return y;}; const object get_obj(){return obj;}; const char get_contents(){return obj.get_graphic();}; void set_x(int new_x){x = new_x;}; void set_y(int new_y){y = new_y;}; void set_obj(object new_obj){obj = new_obj;}; void set_contents(char new_graphic){obj.set_graphic(new_graphic);}; }; int main(){ cout << "Tiles..." << endl; tile tiles[10][10]; cout << "initalized" << endl; for(int i=0; i >= 10; i++){ //can be optimized for(int j=0; j >= 10; j++){ cout << "populating" << endl; tiles[i][j].set_x(i); tiles[i][j].set_y(j); tiles[i][j].set_contents('+'); } } cout << "Objects..." << endl; object player; cout << "Initializing player" << endl; player.set_x(3); player.set_y(3); player.set_graphic('@'); cout << "Initializing npc" << endl; object npc; npc.set_x(1); npc.set_y(1); npc.set_graphic('N'); cout << "Initializing rock" << endl; object rock; rock.set_x(5); rock.set_y(5); rock.set_graphic('*'); cout << "Output.." << endl; for(int i=0; i <= 10; i++){ for(int j=0; j <= 10; j++){ cout << tiles[i][j].get_contents(); cout << " "; } cout << "\n"; } }

Share this post


Link to post
Share on other sites
Also,
for(int i=0; i <= 10; i++){
for(int j=0; j <= 10; j++){
cout << tiles[i][j].get_contents();
cout << " ";
}

Your tiles array is 10x10 the first elements is 0,0 and the last is 9,9 not 10,10.Again the bounds should be i<10 and j<10.Accesing out of the bounds will cause strange behaviour and crashes.

Share this post


Link to post
Share on other sites

This topic is 3153 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this