Return to main

This is a discussion on Return to main within the C Programming forums, part of the General Programming Boards category; Hey guys, I was wondering what I would do to return to the main function. Say if I have a ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    27

    Return to main

    Hey guys, I was wondering what I would do to return to the main function.

    Say if I have a switch, and i've gone to switch 1, finished the business in switch 1 but now want to go to switch 3 or something. How can I allow the user to return to the main menu with return?.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You put your switch in a loop and when you get in the switch case statement you reset the switch counter to where you want to go next and issue a break. However, what you are trying to do seems very fishy to me, it may be worth explaining exactly what you are doing and why this is required.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    27
    What do you mean, "Fishy?".

    It's for a Uni Assignment.

    What I am trying to do is return to the main menu (which is under void main()). I have made an If statement asking whether the user wants to do something else with the program, or not, thus given by 1 or 2.
    If the user enters 1, I want to return the user to the main menu so they can choose for themselves what they would like to do from the list.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    which is under void main()
    That should be int main(), not void main().

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    27
    University says void main().
    So whether it's right or wrong, i'd be losing marks for int main().

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Quote Originally Posted by MstrKurt View Post
    What I am trying to do is return to the main menu (which is under void main()). I have made an If statement asking whether the user wants to do something else with the program, or not, thus given by 1 or 2.
    If the user enters 1, I want to return the user to the main menu so they can choose for themselves what they would like to do from the list.
    Write a function that will display the menu options and get a choice from the user. The function should return the choice value when a sane choice has been made. Then you use that return value to control the program in main.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by MstrKurt View Post
    University says void main().
    So whether it's right or wrong, i'd be losing marks for int main().
    University is wrong. The point of university is to make you think for yourself not to follow blindly.

    The correct prototype for main is:
    int main(void) or int main(int argc, char *argv[]) if you are passing command line arguments to your program. Both of them must return 0 at the end of normal execution or some other value in case of catastrophic errors.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > University says void main().
    Throw this at them
    Index of /jtc1/sc22/wg14/www/docs/n869

    Are they teaching you other crap, like using gets() to read input, or fflush(stdin) to "clean" the input stream.
    If they are, you're in the wrong place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. return value from main
    By theju112 in forum C Programming
    Replies: 9
    Last Post: 08-19-2011, 06:39 PM
  2. Why does main need to return a value?
    By jimboob in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2004, 09:48 PM
  3. return to the main()
    By Dummies102 in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2002, 12:23 PM
  4. return from main
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-11-2002, 12:29 PM
  5. return from main
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-11-2002, 09:04 AM

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