News:

11 March 2016 - Forum Rules

Main Menu

labyrinth loader im having trouble with

Started by InfamousKnight, May 08, 2013, 08:52:45 AM

Previous topic - Next topic

InfamousKnight

So I have the labyrinth maker working just fine I just can't figure out how to read from it.
Here's the code on it:
http://pastebin.com/Z90DtvzV
I know what I'm doing wrong I just can't figure out new lines. I know what getline does but idk how I'm supposed to use a 2D array out of it.

I don't care that my algorithm is a bit messy its just my way of coding. In fact, I believe thats the best way of doing it using those basic factors.

furrykef

Quote from: InfamousKnight on May 08, 2013, 08:52:45 AMI don't care that my algorithm is a bit messy its just my way of coding.
Your way of coding sucks.

KaioShin

You can find out what's wrong with 2 minutes in a debugger looking at what your code actually does.

But I guess debugging is one of those hipster things that no real(tm) programmer needs anyway, right?
All my posts are merely personal opinions and not statements of fact, even if they are not explicitly prefixed by "In my opinion", "IMO", "I believe", or similar modifiers. By reading this disclaimer you agree to reply in spirit of these conditions.

henke37

I smell a lot of duplicated code in that switch statement. Have you considered refactoring the code to avoid the duplication? You are also inconsistent in how you address cells in the array.


InfamousKnight

Quote from: KaioShin on May 09, 2013, 05:36:38 AM
You can find out what's wrong with 2 minutes in a debugger looking at what your code actually does.

But I guess debugging is one of those hipster things that no real(tm) programmer needs anyway, right?
No debugger is going to help. The thing that's wrong with the code is that it reads byte by byte in one line and I have a 2d array which means I have multiple lines in the file. A debugger isn't going to tell me how it's done.

@henke know of any better way of doing this? I suppose I can write a function but then it would still be repeating. Also, I COULD have used else if but in some compilers it's either elseif or its else if and I didn't feel like switching it if I did it wrong.

All I'm asking is for a program to read 400 bytes to a 2d array line by line.

henke37

A high level analysis of the code spots that the code for the four cases do the exact same thing, just with a minor behavior difference. Specifically, the difference is in how the future position is computed. The remaining code is identical. Isolate the different part and factor out the common part.

As for "no debugger is going to help", that's not true. The debugger allows you to follow the code as it executes and will let you spot the implementation error here. It might take a few hits on the step button, but the error here is clearly debugable.

LostTemplar

Quote from: InfamousKnight on May 09, 2013, 11:49:46 AM
Also, I COULD have used else if but in some compilers it's either elseif or its else if and I didn't feel like switching it if I did it wrong.

What are you talking about? If it's C/C++ it's always "else (if)". There's no keyword elseif in the language. And even if you were unsure it's just a matter of trying to compile it, the compiler will tell you if it's wrong. Considering the scale of your programs that's a matter of ten seconds, if that. Also: Syntax highlighting.

Also, about your problem regarding the input of 2-D data, I don't think you're doing it wrong per se. What exactly is the problem? If you want to read line by line from a text file, you can use std::getline to read the next line into an std::string and iterate over its characters.

InfamousKnight

Quote from: LostTemplar on May 09, 2013, 02:53:10 PM
What are you talking about? If it's C/C++ it's always "else (if)". There's no keyword elseif in the language. And even if you were unsure it's just a matter of trying to compile it, the compiler will tell you if it's wrong. Considering the scale of your programs that's a matter of ten seconds, if that. Also: Syntax highlighting.

Also, about your problem regarding the input of 2-D data, I don't think you're doing it wrong per se. What exactly is the problem? If you want to read line by line from a text file, you can use std::getline to read the next line into an std::string and iterate over its characters.

OOPS, I was thinking about my map maker. Sorry. And about the 2-D data thing, what would I use to read a 2D array for getline?

Would this work:
for(int x = 0; x < 20; x++) {
   for(int y = 0; y < 20; y++)
   map
  • [y] = mapfile.getline();
    }
    My programming teacher told me about that mapfile.getline thing. I never saw that in my life.

LostTemplar

That won't work at all. You need to think about a) what arguments getline(...) takes and b) what it returns (hint: it's not what you want).

KaioShin

All my posts are merely personal opinions and not statements of fact, even if they are not explicitly prefixed by "In my opinion", "IMO", "I believe", or similar modifiers. By reading this disclaimer you agree to reply in spirit of these conditions.