Note that there are some explanatory texts on larger screens.

plurals
  1. PORetaining modified values inside and outside of if-else statements?
    primarykey
    data
    text
    <p>My current background in C++ is I've taken a college level course for a semester.</p> <p>I'm having troubles figuring out how to make the values I assign inside if-else statements retain outside of it. I figured if I declare it beforehand outside and use pointers to handle the memory addresses themselves, it would be fair game, but apparently not.</p> <p>my "MAZE" file for now:</p> <pre><code>. # # # # # # . . . . . # # # . # # . # # # . # # . . # # . . # # . # # . # # . . # # . # # . # # . . . # . . . # # . . . # # </code></pre> <p>And this is what I have inside of my maze.h file, defining the constructor. I haven't gotten around to most of the program yet, but my main.cpp already handles calculating the text file's maze dimensions, and then it just calls </p> <p><code>Maze *testMaze = new Maze(rows,columns);</code></p> <p>My maze.h file has bool maze[0][0] listed as a private member. It's set to 0 because at code's first execution, there is no way to know the size of the maze yet. I run code to calculate the size, and then pass the rows and columns in as parameters in the Maze constructor.</p> <p>My maze.cpp file's constructor with parameters:</p> <pre><code>Maze::Maze(int rows, int columns) { string mazeRow=""; int roIndex=0; //Row Index int coIndex=0; //Column Index bool maze[rows][columns]; bool* target = NULL; ifstream input; input.open("MAZE",ios::in); if(input) { while (getline(input, mazeRow)) { //Testprint this row of the maze. cout &lt;&lt; mazeRow &lt;&lt; endl; //mazeRow is the data in that row of the text file. //Store each non-space value. for (coIndex=0; coIndex&lt;mazeRow.length(); coIndex++) //For each character in that row... { char check=mazeRow[coIndex]; target = &amp;maze[roIndex][coIndex]; if (check=='.') //Path { *target=true; cout &lt;&lt; *target &lt;&lt; " "; //These print statements print correctly. } else if (check=='#') //Wall { *target=false; cout &lt;&lt; *target &lt;&lt; " "; //These print statements print correctly. } else if (check==' ') //Space { //ignore spaces } else //For all other cases, an invalid character is present. { cout &lt;&lt; "Invalid character detected." &lt;&lt; endl; } cout &lt;&lt; *target &lt;&lt; " "; //For some odd reason, this line BY ITSELF doubles up print. Ex. instead of printing 1 1 0 1 0, it would print 1 1 1 1 0 0 1 1 0 0. } cout &lt;&lt; "End of row." &lt;&lt; endl; roIndex++; } } input.close(); cout &lt;&lt; "Storage completed." &lt;&lt; endl; for (int i=0; i&lt;rows; i++) { cout &lt;&lt; "Row " &lt;&lt; i &lt;&lt; endl; for (int j=0; j&lt;columns; j++) { /* This is the print test to see if the values are retained outside of the first block. None of them print the maze file properly. */ if (maze[i][j] == true) cout &lt;&lt; maze[i][j] &lt;&lt; "\t" &lt;&lt; "." &lt;&lt; "\t"; if (maze[i][j] == false) cout &lt;&lt; maze[i][j] &lt;&lt; "\t" &lt;&lt; "#" &lt;&lt; "\t"; cout &lt;&lt; "Column " &lt;&lt; j &lt;&lt; endl; } cout &lt;&lt; "End of row." &lt;&lt; endl; } cout &lt;&lt; "Verification completed." &lt;&lt; endl; } </code></pre> <p>First time asking a question here, so if I'm missing anything, let me know.</p> <hr> <p>Just wanted to put up an update here. Turns out, I was either overly stressed, sleep deprived, or both. I put down this code for a few hours and took a new look at it, and I found so many logic errors in my code, including but not limited to forgetting why certain variables existed and what they were used for, not thinking clearly on all the aftereffects of incrementing (++) within a loop, and otherwise sloppy mistakes like those. I've taken several of the answers/comments below and refreshed my understanding of my own code, helping me correct these errors. When I get around to it, I'll supply a finished code if I can.</p> <hr> <p>Solved the problem. I'll be able to self-answer in a few hours and explain what actually happened then.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload