dkdude wrote: |
---|
Alright, I added a do-while statement to the code to control if the program will restart or not... but whenever I input something, it restarts, disregarding if it was "yes" or "no"
Do you see anything wrong with the code? |
There's one minor flaw in your logic here
while (choice != "Yes" || choice != "yes" || choice != "y");
Your code will repeat if choice is not equal to "Yes", or it is not equal to "yes", or it is not equal to "y"
So, if
choice == "Yes"
then that means both
choice != "yes"
and
choice != "y"
are
true.
If you used 'AND', you might get the result you're expecting.
dkdude wrote: |
---|
So are you saying that I should call the variables within the function that they are being used in? |
A good place to put them is next to the code where they're used. if you use a variable in main, then somewhere in main is a good place. If you only use it inside an 'if' or 'while' block, then inside that block is a better place. Keeping variables down to as narrow scope as possible will better help you manage your code, and avoid the situation where you could end up with loads of variables all potentially being changed from hundreds of different areas in your program. (In big projects this can cause major headaches when there are a million+ lines of code which all have access to the same variable). If more than 1 function needs visibility of a variable, then you can write function arguments to pass them around - this gives your program a much better structure and flow.
dkdude wrote: |
---|
Just out of curiosity, why should you not call main()?
|
As for avoiding a recursive call of
main()
, there are several reasons:
1) The C++ standard forbids it (the C++ standard is a document which defines the C++ language, including what C++ programmers are allowed to do and what C++ compilers can and can't accept). Most compilers diverge away from the standard a little bit in places (And there are many things left completely open/undefined), but as a general rule of thumb you should avoid writing code which the standard says is disallowed, unless you have a compelling reason to do so.
2) main is a special function, and your compiler is allowed to do things with it which might not normally happen with other functions (Your program needs an entry point, and the compiler is allowed to do anything it needs to do in order to get it to work). There's a chance that it might behave differently when compared with a normal function, depending on the compiler.
3) Recursion eats up space on your call stack (which is generally limited - more noticably on smaller devices). Calling a function from within itself does not just "jump" to an earlier point in your code, it adds a function call frame to your stack as well - If you keep going, you'll eventually run out of stack space. If you do this with main, then each time you call main, you are effectively starting your program afresh on top of everything which your program did before (Think of it like repeatedly putting up extra layers of wallpaper without taking down the old layers first)
4) On a more general note about recursion - it can be a bit of a tough one to debug and maintain - your code will usually be simpler when you stick to the standard for/while loops. If you use recursion to repeat large chunks of your program, or to jump back-and-forth repeatedly between different functions then you may end up with a complicated web of "spaghetti" code. Recursion is occasionally very useful for particular algorithms, but it needs to be used judiciously in places where for/while aren't up to the job.