Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Wrong placement in 2d vector

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 :

+—+—+—+

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

||||

+—+—+—+

||||

+—+—+—+

||||

+—+—+—+

Program ended with exit code: 0

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading