Yeah... I need to remake my programs.
Yeah... I need to remake my programs.
>> The variable choice is initialized at the top of the function. Thanks for the help.
You mean you changed the code to initialize it now? If you meant that in the code you posted it is already being initialized, then that's not correct. It is defined but not given an initial value. If you mean you changed the code to initialize the value, then never mind this post.
There is definitely a good reason to use goto, but it's also more deprecated in C++. THE best way of using it is to jump to the end of the function to take care of cleanups, especially if that function has a lot of exit points.
In C++, this can also be done with try/catch which might be a better solution, though.
>> THE best way of using it is to jump to the end of the function to take care of cleanups, especially if that function has a lot of exit points.
In C++ that should be done with RAII. It should not be done with try/catch or goto.
Classes to destruct things? All things won't work that way. In Windows apps, there's a lot to do at the end of a function presumably (set window texts, set progress bar progress, manage any threads, etc, etc), so goto or try/catch would be a better choice.
Haven't we had this discussion before?
>> Classes to destruct things?
Yes, that's basically what RAII is. What do you think a smart pointer is? It is a class designed to clean up the pointer when it is appropriate.
>> All things won't work that way.
I disagree. Anything you can do at the end of a function you can do in a destructor. If you have an example of something that can't I'd be willing to listen.
>> (set window texts, set progress bar progress, manage any threads, etc, etc)
All of these should be implemented with RAII or some other mechanism (like an extra function call). A goto or try/catch should not be used for this in C++.
Whatever. You win.
goto is not deprecated in C++ ("deprecated" has a specific meaning in the C++ standard, meaning it is identified for removal from a future version of the standard, so the description "more deprecated" has no meaning).
It is true that C++ provides more alternatives to using the humble goto statement than does C. Although exception handling is actually more comparable to using a setjmp()/longjmp() combination than it is to using a goto.
When using deprecated, I wasn't implying that it's deprecated or going to be removed, but that it should be avoided because better solutions exist, mostly.
I think "bad practice" would be a better term.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Well, recently someone posted code that was structured like this:
As you can see, the structure is seriously messed up: the end part of the program is in the middle of main().Code:loop: do something if some condition end: do something stop program if loop again goto loop goto end
Beginners probably find goto easier to use out of inexperience: being not used to express their ideas in higher-level flow control structures.
Another typical misuse might look like this:
Reason? The programmer has mastered checking conditions and goto and that's about that. This would be much better with if else blocks, or switch and/or using functions when "do something" is long.Code:if A goto doA if B goto doB goto skip doA: do something goto skip doB: do something skip: program resumes here
All programmers can understand higher level control flow structures easily ("That's a for loop, the initial condition is x, end condition is y, increment is z"), but each structure built with goto's is unique and requires a lot of effort to figure out. Given sufficiently large code size (someone misusing goto would probably also have an enormous main function), non-trivial problem, poorly named variables etc and it might be virtually impossible to figure out how the code works and how it is supposed to be modified if you want to change something.
Last edited by anon; 12-02-2007 at 09:18 AM.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
So would this be good for replacing goto in my code?
Code:int main() { bool invalid = false; int choice = 0; cout << "Game Name Here v 1.0\n"; while (choice != 4) { cout << "Main Menu:\n"; cout << "1. Play Game\n"; cout << "2. Instructions\n"; cout << "3. Credits\n"; cout << "4. Quit\n"; do { cin >> choice; switch (choice) { case 1: play(); cout << "Game Name Here v 1.0\n"; invalid = false; break; case 2: instruct(); cout << "Game Name Here v 1.0\n"; invalid = false; break; case 3: credit(); cout << "Game Name Here v 1.0\n"; invalid = false; break; case 4: invalid = false; break; default: cout << "\a"; invalid = true; break; } } while (invalid==true); } return 0; }
Yep, that works. I might set invalid to false before the switch so that you don't have to do it in every case, but otherwise that's just fine. (I'd also check the return value of cin for more error checking as I mentioned earlier, but that's unrelated to this problem.)
It may not seem that much different, but the fact that there is a possible inner loop is now much clearer. Programmers are tuned to look for for, while and do while, but a goto label has any name and doesn't necessarily standout amongst the rest of the code.
Thanks. Now I have to go back and fix my code.