Multi-Dimensional Vector Printing Sideways

This probably sounds really weird but here is what is happening.

I am using SFML along with C++, but I think this problem falls under the C++ category and not the SFML category.

I am using a multi-dimensional vector to hold numerical values that are read from a text file. The different numbers represent different types of terrain on a map (the text file is essentially a map for a game).

I read the numbers from the file and store them in a 2D Vector. The numbers get into the vector in the correct order, but then when I try to print them in the SFML window the map is printed sideways, so I can't get the colors correct on the map.

Inside Text File:
7777777
7000007
7000007
7770777
7000007
7000007
7777777


Inside Vector:
7777777
7000007
7000007
7770777
7000007
7000007
7777777


Inside SFML Window:
7777777
7007007
7007007
7000007
7007007
7007007
7777777


As you can see it has been rotated 90 degrees inside of the window. The code I think that is causing this is the nested for loops.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    char Line[50];
    Import = fopen(Directory.c_str(), "r");

    columns = XDimension();

    clearerr(Import);
    fseek(Import, 0, SEEK_SET);

    rows = YDimension();

    for (int i = 0; i < 10; i++) {
        std::vector<int> row;
        for (int j = 0; j < 20; j++) {
            row.push_back(rows * columns);
        }
        MapArr.push_back(row);
    }

    clearerr(Import);
    fseek(Import, 0, SEEK_SET);

    for (int i = 0; i < rows; i++) {
        fgets(Line, 50, Import);
        for (int j = 0; j < columns; j++) {
            MapArr[i][j] = Line[j];
            if (MapArr[i][j] == 48) {
                Rect = sf::Shape::Rectangle(j*50, i*50, 50, 50, sf::Color::Green);
                //cout << i*50 << " " << j*50 << endl;
                DispMapArr.push_back(Rect);
            }
            cout << MapArr[i][j];
        }
        cout << "\n";
    }


If you need any more information please inform me. :) Thanks.
As you are doing it, you are looping through each i, then printing out the "line".

As stored in the vector, it is as such:
 i ->
j7777777
 7000007
|7000007
V7770777
 7000007
 7000007
 7777777


Hence when you print it out it is "rotated". If you want to print it the other way, simply switch your i/j in your loops (so you loop over j first, then i) or switch the i/j in the array.

Personally, I would suggest the former.
Last edited on
Okay I switched them in the array, but not its printing out the ASCII values which wouldn't be a problem if they weren't the wrong ASCII values.

It is printing:
7777777
0000000
0000000
0000000
0000000
0000000
7777777


Should be:
7777777
7000007
7000007
7770777
7000007
7000007
7777777


EDIT: Okay I got it to print the correct ASCII values, so the map shape is correct, but the map isn't being colored correctly by the if statement inside of the inner for loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
    for (int j = 0; j < rows; j++) {
        fgets(Line, 50, Import);
        for (int i = 0; i < columns; i++) {
            MapArr[i][j] = Line[i];
            if (MapArr[i][j] == 48) {
                Rect = sf::Shape::Rectangle(i*50, j*50, 50, 50, sf::Color::Green);
                //cout << i*50 << " " << j*50 << endl;
                DispMapArr.push_back(Rect);
            }
            cout << MapArr[i][j];
        }
        cout << "\n";
    }
Last edited on
Topic archived. No new replies allowed.