If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Loops for game: help?

Hello, today I was working on a basic text 'game' (if you can call it a game at this point) to practice some basic techniques with loops, strings, arrays, etc., and what I would like to happen is that every time the user puts in input, it activates something (like 'move north' moves the player north (duh) or 'take note' picks up a note off the ground and prints the text to the console). I know that there should be some way to do this with loops, but at my skill level I'm not exactly a pro with loops, so if possible could somebody tell me which loop would be best for my explained situation and why? Thanks in advance guys.

(PS I tried a 'while' loop and it didn't exactly work out too well, will post code with the while loop if someone asks to see it)

Re: Loops for game: help?

Well, I finished the program with absolutely everything inside of a 'while' loop, and that ended up prompting me to enter in the string that I needed, but after I entered anything in, the program wouldn't output any text or change any variables, just prompting me to enter in the string again, then closing.

The second time, I only put the prompting for the user to enter a string inside the while loop and put everything outside, but now if I try to open the program it closes immediately after

Both times the compiler did not warn me about any problems with the code or anything.

(PS: I didnt know that this section was for visual C++ only, I will post in the normal C++ area next time.)

(PSS: How do you put the code into one of those boxes? Sorry I'm new to these forums :S)

Re: Loops for game: help?

Re: Loops for game: help?

Originally Posted by momrocker

Well, I finished the program with absolutely everything inside of a 'while' loop, and that ended up prompting me to enter in the string that I needed, but after I entered anything in, the program wouldn't output any text or change any variables, just prompting me to enter in the string again, then closing.

The second time, I only put the prompting for the user to enter a string inside the while loop and put everything outside, but now if I try to open the program it closes immediately after

You shouldn't be guessing about what goes in the loop; just decide how you want the control flow to work and structure the code accordingly. If you're having trouble figuring out what's going wrong with your planned control flow, then set a debugger breakpoint, and step through the execution a line at a time.

Both times the compiler did not warn me about any problems with the code or anything.

The compiler is only able to warn or error for problems detected by static code analysis. Even then, it isn't doing a comprehensive analysis, just flagging a few issues it has been programmed to notice. It cannot deduce what you are trying to do and tell you whether or not it will work the way you've written it.

(PSS: How do you put the code into one of those boxes? Sorry I'm new to these forums :S)

[code]Code here[/code]. I used a particular trick to make that show up in plain text, but normally if you write that it will do this:

Re: Loops for game: help?

Originally Posted by Lindley

You shouldn't be guessing about what goes in the loop; just decide how you want the control flow to work and structure the code accordingly.

I said I wasnt exactly an expert with loops (aka a lighter way of saying i basically know nothing :P)
anyways heres my code, its a bit umm.... well just look and see lol. I could have probably put all of the stuff in main in the 'while' loop in a separate function to clear things up, and i know as soon as i post this i will be able to hear every single expert C++ programmer do a /facepalm, but here it goes...

I put alot of pointless comments in here as i tried to explain to myself how it all works, so you could probably ignore those.

NOTE: ok, i found out that i had '=' instead of '==' for the section which checks for error. but now i have the problem that i have to hit the enter button an ungodly amount of times for text to appear, and some input for the string dont work, like south.

Re: Loops for game: help?

Ok, through about 20 minutes of looking through the program's source code, I eventually changed a few parts that were messed up (such as '=' instead of '==' or extra 'cin.ignore();'s where they dont need to be) and now it works like a charm. If anyone cares enough they can use the code I left up. Oh, and if there are any admins on this forum that could close this thread that would be greatly appreciated.