Home > other >  Wrong placement in 2d vector
Wrong placement in 2d vector

Time:10-18

I'm trying to print a full closed maze (where the user inputs width and height), but when I print the maze the "|" walls are not placed correct. Why is this, because the parameters are set. Also the right amount of "|" are placed but at wrong positions

    int vectorLength = (userRows * 2)   1;
int vectorWidth = (userColloms * 4)   1;


std::vector<std::vector<std::string>> maze(vectorWidth, std::vector<std::string>(vectorLength, ""));


for (unsigned int i = 0; i < vectorLength; i  ) {
    int testj = 0;
    for (unsigned int j = 0; j < vectorWidth; j  ) {
        if (i % 2 == 0){
            if (j == 0 || j == 4 || j == 8 || j == 12 || j == 16 || j == 20 || j == 24 || j == 28 || j == 32) {
                maze.at(j).at(i) = " ";
            } else {
                maze.at(j).at(i) = "-";
            }
        }
        if (i % 2 != 0){
            if (j == 0 || j == 4 || j == 8 || j == 12 || j == 16 || j == 20 || j == 24 || j == 28 || j == 32) {
                maze.at(j).at(i) = "|";
            }
        }
        
    }
    
}

The output for input 3 3 :

--- --- ---

||||

--- --- ---

||||

--- --- ---

||||

--- --- ---

Program ended with exit code: 0

CodePudding user response:

It looks like your code works by replacing characters in your maze vector. Since your vector is initialized all to empty strings, there is nothing between each "|" to give space. You should either initialize your vector to be full of spaces " " or find another way to pad the space between each bar.

  • Related