Thread: restart the program

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    restart the program

    Is there a function that will restart the program at the main function?

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    put a do{}while() loop or just a while() loop at the start of main and at the end of whatever you want.

    For example:

    Code:
    ...
    int main()
    {
         bool quit = false;
         while(quit == false)
         {
              ...whatever you want to repeat
              (when you want to quit just set the quit flag to true)
              .....
          }
         return 0;
    }

    Good luck!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    restart program

    is there something to use besides a do while loop to restart the program. Maybe a function call?

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    why did you make another thread? Well my answer's there =P
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The loop is the appropriate way of doing it.

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by MacGyver View Post
    The loop is the appropriate way of doing it.
    oh really? so a loop will call all constructors of static variables and re-initialise all the C runtime stuff?

    kellymart, the problem with restarting a program is that as soon as you call the function to exit the program... well the program exits, so your restart won't get called. This is why we tend to avoid restarting programs. However, if you REALLY need to restart, one way is to spawn another program that will spawn your program back again.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by ChaosEngine View Post
    oh really? so a loop will call all constructors of static variables and re-initialise all the C runtime stuff?

    kellymart, the problem with restarting a program is that as soon as you call the function to exit the program... well the program exits, so your restart won't get called. This is why we tend to avoid restarting programs. However, if you REALLY need to restart, one way is to spawn another program that will spawn your program back again.
    I don't think respawning the same program is better if you can do it via a loop.
    Last edited by MacGyver; 04-17-2007 at 08:35 AM.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    The really bad way to do it is to call the main function. Although some real old compilers allow this, it is not advised.

    Go with Chaos Engine's theoy instead.

    Oh, on a side note, using a loop would only repeat the section of code until you break the loop. Unless you put a loop round the whole code ( not advised ) then you could call the function that main calls first to set reset or set up the variables in a recursive mannor. To be honest, restarting a program is hardly done in practice
    Double Helix STL

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    My guess is that spawning another process is overly complicated for what kellymart87 wants.

    If you can do it with a loop, then you should definitely prefer a loop over any other option. You can always move code into a separate function or class to make the loop look nicer and clearer, but in the end you will probably find the loop to be the best option.

  10. #10
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Yeppers, that's what i told you from square one haha
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  11. #11
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    well, maybe if the OP said why he wanted to restart we could make a better judgement. If you read my post, you'll notice I specifically said restarting is in general to be avoided. However, there are some case where it's unavoidable (or at least, so much hassle that it's easier to restart).
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  12. #12
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Yeah i know what you're saying.

    I use 'restarting' (to use it loosely) all the time with programs which have menus. For instance, the whole program (for the most part cept for certain variables) is in a while loop. Each time the loop is called it displays the menu, one of the menu options is quit which breaks the loop. That's the only time i've needed to use a 'restart.'

    So yes, could we perhaps know Why you need to restart the application?

    Thanks!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    630
    In case user finds the virus and wants to stop it.

  14. #14
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    ???

    Now i'm lost.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  15. #15
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    The poster PM'ed me and the problem is fixed =)
    "Anyone can aspire to greatness if they try hard enough."
    - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM