Like Tree1Likes
  • 1 Post By manasij7479

jUST LEARNING FUNCTIONS

This is a discussion on jUST LEARNING FUNCTIONS within the C++ Programming forums, part of the General Programming Boards category; I am just learning functions. I understand that the code Code: void MegaMillion (void) is a function that takes no ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40

    jUST LEARNING FUNCTIONS

    I am just learning functions. I understand that the code

    Code:
    void MegaMillion (void)
    is a function that takes no input and returns nothing to the caller, but if I have:

    Code:
    int main ()
    {
        Start:
        char z;
            cout << "For Florida MegaMillion press #1" << endl << endl;
            cout << "For Florida Lotto press #2" << endl << endl;
            cout << "For National Power-Ball press #3"  << endl << endl;
            cout << "To start over press #4" << endl << endl;
     cin >> z;
    
     if (z == 1)
         {
           cout << "You have chosen to play Florida's MegaMillion Lotto," << endl << endl;
    MegaMillion();
        }
    
     else
     if (z == 2)
         {
           cout << "You have chosen to play the Florida Lotto." << endl << endl;
    FloridaLotto();
        }
    
     else
     if (z == 3)
        {
           cout << "You have chosen to play the National Power-Ball Lotto," << endl << endl;
            PowerBall();
        }
     else
     if (z == 4) goto Start;
     else
     if (z != 1 && z != 2 && z != 3 && z != 4)
        {
            cout << "You have chosen to end the program." << endl <<    endl;
            exit();
        } 
         return 0;
    }
    where I'm trying to get rid of all the gotos, how do I write all the goto replacement functions? What do I place inside the ()? What do I do with the label "Start?" What belongs in the () of the exit code?
    . . . . therry

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If the functions take no parameters, then you put nothing in the ().
    You replace Start and goto Start with a loop (perhaps a do while loop).
    If you do it properly, you won't need the exit call at all.

    What book/website are you learning from? They shouldn't have told you to use goto's. They should have taught you about loops and functions. Get a better book/website.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    The best way is to not put the goto in there to begin with.
    An easy way to do that is when starting out with writing the program, put the loop in first, and then gradually add the stuff inside it.
    In other words, more top-down thinking, less bottom-up thinking.

    In this case a do .. while loop is the most appropriate, and you want the loop condition to be while z == 4
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    you have declared your control variable z as a char, but then you test for number values.

    change it to an int, or it could be unsigned for a very clear indication that z is never intended to hold a -value.

    Also consider renaming it, single letter variables are ok for some things, there are accepted i,j,k for example that is a common convention for nested loops. But in the context of your program ' z ' does not really say anything. Better to write ' choice ' or 'gameChoice' or ' draw ' maybe.

    On another note I find it interesting seeing people use the ' exit ' call in recent threads, is this some kind of hangover from another language? I appreciate it is probably a standard keyword but i only ever use it in shell stuff, and have never considered it in my own C or C++, stuff, I assume it breaks the whole program execution, even from within a called function.. If so then I would advise the OP uses return or break calls in the loop if such a thing be neccesary.

    By the way... my very first post on the cboard was also a lottery game question... was nice to see this one.. good luck!
    Last edited by rogster001; 04-07-2012 at 12:30 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by rogster001 View Post
    On another note I find it interesting seeing people use the ' exit ' call in recent threads, is this some kind of hangover from another language? I appreciate it is probably a standard keyword but i only ever use it in shell stuff, and have never considered it in my own C or C++, stuff, I assume it breaks the whole program execution, even from within a called function.. If so then I would advise the OP uses return or break calls in the loop if such a thing be neccesary.
    What is wrong with exit ?
    I thought it was a fabulous way of ending a program (process, more accurately, afaik) without getting a flaggy mess when required !
    (The only time when it can mess up, I think, is when used with system calls like clone(), or some of the exec functions )
    MK27 likes this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by rogster001 View Post
    On another note I find it interesting seeing people use the ' exit ' call in recent threads, is this some kind of hangover from another language?
    How would that make sense? A process is a process, and exit is exit, regardless of the language. Which is to say, your criticism does not have any added meaning in C/C++. Perhaps the reason you see it more appropriate to shell stuff is because the shell stuff you do is smaller in scale?

    Returning an error or throwing an exception should be preferred where applicable, but that is not really an issue specific to using exit() -- it's just a principle that, eg, a library function should not just end a process for whatever reason. In situations where it is appropriate to end the process (because it cannot continue, or it would not make sense if it did), I agree with manasij: unless there is some particular reason not to use exit, that's what exit is for (even if you wrap it together with whatever else).

    That's just as true in shell programming as in C/C++, I think.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    The shell stuff is pretty much smaller scale yes, but I meant that perhaps the op was using exit and would get a surprise when the whole program ends, instead of just returning from the function, unless thats what happens with exit in c languages, like i say i have never written that line. I was not criticising either.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    exit() does not allow objects to be properly destroyed. it simply ends execution right away. a good example of why this is bad is if your program creates files for temporary use (not necessarily the same thing as temporary files), and is supposed to remove them upon destruction of certain objects, those files will never be removed if you call exit(), and will eat up disk space over time. atexit() was created to handle situations like this, but I think that it would be incredibly bad practice to start sprinkling atexit() calls all over your code, especially as the scale of the project starts to get larger. in my programs, I have a specific type of exception (that isn't derived from std::exception), and that exception type only gets caught in main(). I don't ever use catch (...), except below the one catch (TerminateProgramException) because it doesn't give any meaningful information. it only tells you that something was thrown. there is no way to find out what it was (afaik). this ensures that when I want to end my program in a way consistent with RAII, I throw an instance of TerminateProgramException, which gets caught in main, which simply returns, and things get destroyed as they are supposed to.

  9. #9
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Elkvis View Post
    exit() does not allow objects to be properly destroyed.
    Can't something be done at a lower level ? By coupling signal handlers with the destructor(of a resource manager..say) instead of atexit ?
    //My understanding of it is superficial, at best... so apologies if the above statement lacks substance,
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    in theory, you could do that, but exit() is generally an operating system call. it simply instructs the system to terminate the current process. what you suggest would still necessarily require at least one call to atexit().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Functions - Still Learning
    By Alexander in forum C Programming
    Replies: 52
    Last Post: 09-27-2008, 01:57 AM
  2. Learning Dos and learning Windows
    By blankstare77 in forum C++ Programming
    Replies: 8
    Last Post: 07-31-2005, 03:48 PM
  3. Learning..........C
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 12-07-2001, 12:43 PM
  4. API learning reference (for those who are learning)
    By steve_i83 in forum Windows Programming
    Replies: 1
    Last Post: 11-04-2001, 12:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21