Jump to content
  • Advertisement
Sign in to follow this  
Strings

Problem with displaying text (C++)

This topic is 3435 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[j].set_x(i); tiles[j].set_y(j); tiles[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[j].get_contents(); cout << " "; } cout << "\n"; } }

Share this post


Link to post
Share on other sites
Advertisement
Also,
for(int i=0; i <= 10; i++){
for(int j=0; j <= 10; j++){
cout << tiles[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
I realized that when it was outputting extra text.
It's a good thing this matter was addressed before I started writing to the arrays. Thanks again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!