Thread: Why is goto the devil and exiting programs question.

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    36

    Why is goto the devil and exiting programs question.

    Why should not we use "goto"? I am a programming newb and for the following task I would consider it practical.


    Task:
    1. Get user input
    2. If the input is out of a desired range, end the program before doing something else

    This example does NOT use goto but splits up these two choices into an if else structure.

    Code:
    //
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    
    
    int money;
    
    
    
    
    int main()	{
    	cout<<"Enter amount of cents (1-99): ";
    	cin>>money;
    
    
    	/* -----Exit Program if money out of range----- */
    	if (money < 1 || money > 99)	{
    		//Print out error and quit program
    		cout<<"<ERROR> Enter a value between 0-99. "<<endl;
    		
    	return 0;
    	}
    
    
    
    
    	/* -----Else continue----- */
    	else	{
    	//Do something
    		
    	return 0;
    	}
    
    }
    Would not using a goto to "hop" straight to the end more practical? Also what is the most elegant way to exit a program when used on a simply program like this?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Goto can jump all over the code, making it difficult to read and maintain.
    As for the question: loops. The very basic of basics.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    goto encourages people to write spaghetti code which is hard to read and maintain. While there are cases where goto can make the code more readable, those cases are very rare (especially in C++).

    >> Also what is the most elegant way to exit a program when used on a simply program like this?
    returning from main() is probably the best way to go. In your program, I would modify it to look more like this though:
    Code:
    int main()  {
        int money;
        cout<<"Enter amount of cents (1-99): ";
        cin>>money;
    
        /* -----Exit Program if money out of range----- */
        if (money < 1 || money > 99)    {   
            //Print out error and quit program
            cout<<"<ERROR> Enter a value between 0-99. "<<endl;
        }
        else    {   
            //Do something
        }   
        return 0;
    }
    Note that there is just one return statement at the end, and I got rid of the global variable. Global variables are also considered bad design as well.
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    36
    Yeah I see using one return at the end after the if-else does make more sense. However why is goto part of c++ when every book tells you to stay away from it like hell? It just looks so "practical" for a newb like me.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Jefff View Post
    Yeah I see using one return at the end after the if-else does make more sense. However why is goto part of c++ when every book tells you to stay away from it like hell?
    Because it is in the C language, and C++ tried to stay backwards compatible to C.
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    @OP
    The only place where goto is advisable is coming out of nested loops.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    36
    This is another example on when I think goto would have been practical:
    Code:
    /* Exercise 7.1 */
    /*--------------------
    Purpose:
    A program that converts English to metric for various different units.
    
    Units supported:
    Miles > Kilometers
    Gallons > Litres
    
    
    --------------------*/
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    void milesconvert();
    void gallonsconvert();
    
    
    int main()	{
    
    
    	int x;
    	//Create a menu
    	cout<<"Available conversion types:"<<endl;
    	cout<<"<1> Miles to kilometers"<<endl;
    	cout<<"<2> Gallons to liters"<<endl;
    	cout<<"<0> Exit program"<<endl;
    	cout<<"Select type: "<<endl;
    	cin>>x;
    	
    	if (x < 0 || x > 2)			{
    		//Exit program if value out of range
    		cout<<"<ERROR> Value out of range - Exiting."<<endl;
    	}
    	//else continue
    	else	{ 
    		switch (x)	{
    
    		case 0:
    		//Exit program
    		cout<<"Exiting"<<endl;
    		break;
    
    		case 1:
    		milesconvert();
    		break;
    
    		case 2:
    		gallonsconvert();
    		break;
    
    	}
    }
    
    	//Exit
    	return 0;
    }
    
    
    
    
    //Miles function	1 miles = 1.609344 kilometers
    
    void milesconvert ()	{
    	double miles;
    	cout<<"Enter amount of miles: ";
    	cin>>miles;
    	
    	miles = miles *	1.609344;
    	cout<<"Amount of kilometers: "<<miles<<endl;
    }
    
    
    
    //Gallons function	1 US gallon = 3.78541178 liters
    
    void gallonsconvert ()	{
    	double gallon;
    	cout<<"Enter amount of gallons: ";
    	cin>>gallon;
    	gallon = gallon *	3.78541178;
    	cout<<"Amount of liters: "<<gallon<<endl;
    }
    Now I feel tempted to put a goto at the end of each function that leads the user back to the original menu. Seems practical. Would it be a really bad idea to do this?
    I am not trying to jump around on this question, but I really want to know why I am not supposed to use it.

    I think the alternative to it would be to pack the whole menu into a function itself that is called at the end of the other functions, but that does not look thaaat more elegant to me.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Jefff View Post
    Now I feel tempted to put a goto at the end of each function that leads the user back to the original menu. Seems practical.
    Apparently you use "practical" the way the rest of us use "impractical". Why make a bunch of different gotos, from a bunch of different places, when you've already got a loop structure available?
    Quote Originally Posted by Jefff View Post
    Would it be a really bad idea to do this?
    I am not trying to jump around on this question, but I really want to know why I am not supposed to use it.
    Given that what you are trying to do is exactly the function of the keyword "while", it would in fact be an extremely bad idea to use goto here.

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    36
    Apparently you use "practical" the way the rest of us use "impractical"
    No need to get offensive, I am just trying from a beginner standpoint to understand the reason behind the not-using of goto. I perfectly understand that an experienced c++ programmer has reasons to avoid goto. I would like to know why. That is why I am asking.
    Last edited by Jefff; 08-05-2009 at 12:10 PM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Jefff
    Now I feel tempted to put a goto at the end of each function that leads the user back to the original menu. Seems practical. Would it be a really bad idea to do this?
    Give it a try and see if you can even do that. (Hint: you cannot goto a label in a different function.)

    Quote Originally Posted by Jefff
    I am not trying to jump around on this question, but I really want to know why I am not supposed to use it.
    Suppose you were able to do what you wanted to do. You would effectively tie the implementation of each of those functions to the implementation of the main function. This is bad for reusability and maintenance.

    Quote Originally Posted by Jefff
    I think the alternative to it would be to pack the whole menu into a function itself that is called at the end of the other functions, but that does not look thaaat more elegant to me.
    Instead of trying to use mutual recursion, use iteration: while the user has not selected the exit option, display the menu, read the option, etc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Jefff View Post
    No need to get offensive, I am just trying from a beginner standpoint to understand the reason behind the not-using of goto. I perfectly understand that an experienced c++ programmer has reasons to avoid goto. I would like to know why. That is why I am asking.
    Well, I think that was my point: you could have one thing, or you could have seven. Which one seems like more work to you? Why would you think doing the same thing in half a dozen different places (meaning if anything needs to change you need to change them all) would be practical?

  12. #12
    Registered User
    Join Date
    Jul 2009
    Posts
    36
    Suppose you were able to do what you wanted to do. You would effectively tie the implementation of each of those functions to the implementation of the main function. This is bad for reusability and maintenance.
    Ok. *writes down note*

    Instead of trying to use mutual recursion, use iteration: while the user has not selected the exit option, display the menu, read the option, etc.
    Would putting it all into a menu function be considered bad programming? Or is that just a recommendation for a different style?

    For me, using the main function only as a "function caller" would be the most "tidy" way of doing it. I find while loops hard to read but that might be just my inexperience. So far I mostly resorted to for loops when looping.

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Would putting it all into a menu function be considered bad programming?
    No, that would be considered good programming. Anytime you put a logical action into its own function, that is a good thing.

    I find while loops hard to read but that might be just my inexperience.
    You will get used to them. It's impossible to create any sort of non-trivial application without loops.
    bit∙hub [bit-huhb] n. A source and destination for information.

  14. #14
    Registered User
    Join Date
    Jul 2009
    Posts
    36
    You will get used to them. It's impossible to create any sort of non-trivial application without loops.
    Yes, but I almost exclusively used for loops. Which look similar but easier to read.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Jefff
    Would putting it all into a menu function be considered bad programming?
    A function should do one thing and do it well. If you want to define a function to display the menu (and return the selected option), that would be a good idea.

    EDIT:
    Quote Originally Posted by Jefff
    Yes, but I almost exclusively used for loops. Which look similar but easier to read.
    They are easier to read than while loops when they are more appropriate than while loops, but otherwise that would be false.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed