What am I doing wrong? I have been doing super simple C++ for a while but when I got the idea to actually try and make something with it, I wasn't sure where to start. But it's very basic on what it's got to do, I just don't have the knowledge to fix it and my book that I'm learning from doesn't help me.
Here's the code:
The problem is in the end of the 'do' loop where I have tried to end it with a 'while' loop. I try to run the program as I have it but no matter what the user input is, everything repeats.
while (move != 4 ||move != 3 ||move != 2 ||move != 1);
This statement will always evaluate to true, because one of the conditions that you're testing for will always be true. There is no way that move can be equal to 1 and 2 and 3 and 4 all at the same time, so move will always be not equal to some of those values.
These are just plain ugly and not really scalable - what if you had 10 menu options?
The fact that a do loop always executes at least once should not be the motivation for preferring them over a while or for loop IMHO. K&R (who invented C) don't like them either. There are some situations where they are valid, but I don't think this is one of them. All 3 types of loops can be re-written in one of the others forms, so that is what I prefer to do with do loops - sometimes at the price of 1 extra variable.
I would have a boolean controlled while loop with a switch inside which has a quit case and default clause to deal with bad input:
Seen as there is only a small amount of code in the switch cases, I just left it there, but normally one would call a function from each of the cases if there was more than say 10LOC.
I'm not very far in C++ so I'm not familiar with using 'void' things.
But I noticed when I tried to see what your code did, Visual Studio 10 had complaints about declarations.
Was there supposed to be #include <iostream> or usingnamespace std;?
The 'switch' is something I have learned recently and have a better understanding of it than things I have learned earlier. I understood what it was doing but as I have said in my second paragraph;
complaints about declarations
Can anyone help me get my original code to a format I can understand, or do I need to go further into the book?
I didn't post all of the code - just enough to give you an idea. Clearly there has to be include's & main etc. I leave it up to you to insert my code into yours & make any necessary changes, although you should note that lines 10 -16 in your code won't be needed. Line 1 of my needs to go before main() in your code.
void is a return type and just means the function doesn't return anything. The ShowMenu function just prints out the menu, so doesn't need to return any value.
You are nearly there, have a go at inserting my code, then post it so we can see how you went.
@ superRazor:
First things first. I definitely prefer the coding of TheIdeasMan.
But you wanted keep your original coding, this is how I changed/editied your code: