My last question was far too wordy and I tried using an entire program to describe my problem. I have since made my issue into 40 lines, namely because I fixed the last issue, and because this is easy to show in 40 lines.
The largescale problem keeps on segfaulting, specifically when current == 25. I don't think I am referencing any memory that doesn't exist, but somehow I keep on getting segfaults. This program in particular won't even compile, but yet my multi-thousand line program will at least make it to 24 before segfaulting at 25.
#include <ncurses.h>
int movementReturn(int current);
int main()
{
int area[5][5];
for(int j = 0; j < 5; j++)
{
for(int k = 0; k < 5; k++) { area[j][k] = (k+1)+(j*5); }
}
int current = 1;
while(TRUE)
{
current += movementReturn(current);
printw("\n%d",current);
printw("\n");
}
}
int movementReturn(int currentPos)
{
if((currentPos + 5) < 26) { printw("Type 1 to move downwards.\n"); }
if((currentPos - 5) > 0) { printw("Type 2 to move upwards.\n"); } //these work this way because I was a moron and programmed one of the variables wrong.
if(currentPos % 5 != 0 and (currentPos + 1) < 27) { printw("Type 3 to move rightwards.\n"); } //basically up is down and down is up which is like
if((currentPos - 1) % 5 != 0 and (currentPos - 1) > 0) { printw("Type 4 to move leftwards.\n"); } //unfortunate, but nonetheless it works and runs, and doesn't really need changing.
int moveInput = (getch() - '0');
if(moveInput == 1 and ( (currentPos + 5) < 26) ) { return 5; } //we need to make the if statements stop the increase to stop segfaulting in case the user types something they shouldn't.
elseif(moveInput == 2 and ( (currentPos - 5) > 0) ) { return -5; }
elseif(currentPos % 5 != 0 and moveInput == 3 and ( (currentPos + 1) < 26) ){ return 1; }
elseif((currentPos - 1) % 5 != 0 and moveInput == 4 and ( (currentPos - 1) > 0) ) { return -1; } //now the player can't move back once they reach an area divisible by 8.
else { printw("You can't go that way\n"); return 0; }
}
Because I don't have ncurses, I changed your printw calls to printf calls, and changed the getch call to just be scanf (#included <cstdio> or <stdio.h>).
I don't see any particular place in your program that would segfault. Some confusing if-statement logic, but no segfaulting. Note that your area array is never used for anything useful (only assigned to).
If you can't get your code to compile, I suggest posting the compilation/linker errors.
That's not helpful. I'm just trying to understand what's going on, and I feel it is best you also understand what is going on.
Edit:
To what Ganado said, yeah, I am unsure of what is causing it to crash as well. I did find out it wasn't compilation time segfaulting, but rather when "current" reaches 25. Current was set to 25 by default when I tried debugging it on my own, but that has since been ineffective.
Oh. Right, arrays start at 0. I guess that kind of flew to the back of my mind. I could spend the next ten minutes explaining myself but I'm sure you understand, thank you for pointing out my awfully obvious error.