-5

I have a problem. I made a "Find the Flowers" program in C++ on repl.it (Online IDE for many languages), and I kept running it every time I made a change to made sure it worked, and when the entire game was finished, I ran it a few times. Everything went well. I was running this on a school Chromebook. At home, I got onto the project on my home laptop and a kept getting the "Segmentation Fault" error. The errors only started happening after I moved to my home laptop. I searched around and found out that "segmentation faults" are when your program is trying to access it "doesn't own". I tried moving to a different Mac, it wouldn't work. The next day, I went back on the school Chromebook, and it didn't work. I have no idea if it's because I have a 10x10 grid (char Map[10][10] = {10x10 grid goes here}), or if it's because I'm calling functions from inside other functions. Please help. The link below will send you to my repl.it code.

https://repl.it/@AndrewBota/Find-The-Flowers

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 4
    Segmentation fault is one of possible manifestations of undefined behavior (another possible manifestation, is, as you already saw: working correctly). Which, in relation to arrays, might be due to you indexing out of bounds of said array. However, without seeing [mre], it's impossible to be certain. – Algirdas Preidžius Dec 03 '19 at 22:03
  • 2
    Welcome to Stack Overflow. In general, you should post your minimal complete example itself, not a link to it. In this case, how do you ensure that the player will not move off the map? – Beta Dec 03 '19 at 22:16
  • Also you need to initialize variables like the player position and number collected. – Ian4264 Dec 03 '19 at 22:20
  • Normally I would copy your code from the link to the question for you, but there is no damn way I'm clicking an anonymous link and hoping to find only code waiting for me at the other side. – user4581301 Dec 03 '19 at 22:33

2 Answers2

1

In your Move() function where you test which of the WASD keys is being pressed your code reads like this

if (PlayerMove == 'W' || 'w')

which always evaluates to true, since 'w' is converted to true as a boolean. This is what you probably meant:

if (PlayerMove == 'W' || PlayerMove == 'w')

In the future you might want to add boundary detection so out of bounds referencing won't occur.

Note: A lot of people are commenting on the fact that you should explicitly initialize the player's position. They are correct that you should, but since you declared them in the global scope, they will deterministically be zero initialized already. See https://stackoverflow.com/a/6032889/8498150

Peter Cheng
  • 758
  • 7
  • 9
1

Just wrote a long answer but it didn't save when I logged on so here is the short version

You never set the initial position for player position. This means that when you assign the variables PlayerX and PlayerY, you just pull from whatever was already there. Sometimes this happens to work fine.

so set an initial starting position

Another thing you need to do is limit movement to be between the grid, so i would suggest a %10 everytime you update player position