How would I fill a two-dimensional array up with numbers.
map_height = 9
map_width = 8

I have this code:

for (i=0; i<map_height; i++)
                {
                    
                    for (p=0; p<map_width; p++)
                    {
                        room_numbers[i][p] = w;
                        w++;
                        }
                    }

But when I try to print it out it comes up with wierd numbers I printed with:

for (int a=0; a<map_height; a++)
                {
                    for (int b=0; b<map_width; b++)
                    {
                        cout << room_numbers[a][b]<< " ";
                        }
                        cout << "\n";
                    }

Did you initialize w? What are the weird numbers that are printing out? Could you post more of the code?

> How would I fill a two-dimensional array up with numbers
Exactly the way you do it.
> But when I try to print it out it comes up with wierd numbers I printed with:
If you give us more context, we might spot a problem.

Thanks
I know it is pretty bad but i'm just re-learning from what I learnt last time I looked into C++.

class Map
{
      public:
             Map();
             ~Map();
             void Map::print();
      private:
                int i;
                int p;
                int w;
                int map_height;
                int map_width;
                int total_rooms;
                int room_numbers[0][0];
                string room_titles[0];
                string room_description[0];
                };
                
      Map::Map()
      {                
                map_height = 9;
                map_width = 8;
                total_rooms = map_width*map_height;
                room_numbers[map_height][map_width];
                room_titles[total_rooms];
                room_description[total_rooms];
                w = 0;
                for (i=0; i<map_height; i++)
                {
                    
                    for (p=0; p<map_width; p++)
                    {
                        room_numbers[i][p] = w;
                        w++;
                        }
                    } 
                }
                
      Map::~Map()
      {
                 }
                 
      void Map::print()
      {
      for (int a=0; a<map_height; a++)
                {
                    for (int b=0; b<map_width; b++)
                    {
                        cout << room_numbers[a][b]<< " ";
                        }
                        cout << "\n";
                    }

                } 
                         
int main()
{
    Map map;
    map.print();
    cin.get();
}

Numbers that come up are nine rows of: 64 65 66 67 68 69 70 71
Different map_width and map_height combos get different numbers.

You're probably thinking that you're resizing array's here:

int room_numbers[0][0];
string room_titles[0];
string room_description[0];
[....]
                total_rooms = map_width*map_height;
                room_numbers[map_height][map_width];
                room_titles[total_rooms];
                room_description[total_rooms];

But you're not. If you want to use dynamically resizable arrays, you need to do it with new and delete or better yet: use std::maps or nested std::vectors

YEah I thought that might be it.
is there any way to make those map_width and map_height variables into constants under the private: thing?

Thanks

YEah I thought that might be it.
is there any way to make those map_width and map_height variables into constants under the private: thing?

Thanks

Sure, you can have constants in a class like so:

static const int width = 100;

Or if you just want some numbers to put into the array you could also do something like this:

enum { width = 100 };

Both work fine.

Oh, That is exactly what I wanted.
All I needed to do was add add static twice to my original code.
Probaly should have given you that code first rather then my "fixed" one
Thanks a heap-a-lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.