Restart

This is a discussion on Restart within the C++ Programming forums, part of the General Programming Boards category; how could i restart a console application? if(...) { ... } else { <restart program here> }...

  1. #1
    Unregistered
    Guest

    Question Restart

    how could i restart a console application?

    if(...)
    {
    ...
    }
    else
    {
    <restart program here>
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    29
    This is prpbably not what you want, I am new to C++. I don't know about restarting a console app but is keeping it going any use to you?
    -------------------------------------------
    char user_option = 'Y';
    While (user_option == 'Y' || user_option =='y'){

    code in here

    cout<<"Continue program Y or N";
    cin >> user_option;
    }
    ---------------------------------------
    Hope this helps!
    -Colin

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Put all the code inside your main in a while loop or do..while loop. Then, if the user wants to quit, break out of the program. At least that's the way I'd do it. However, make sure you free all allocated memory at the end of the loop.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    Thats what win32 apps does to... msg-loop...

  5. #5
    wierd guy bart's Avatar
    Join Date
    Aug 2001
    Posts
    87

    Talking

    just call main().

    Code:
    #include <iostream>//for cout and cin
    char makeLower(char letter);
    int YorN();    //ahh, the yes or no question
    
    ///////////////////
    int main()
    {
      cout<<"hi, do you want to restart";
      if(YorN())
      {
      main();   //restart
      }
     
      return 0;
    }
    /////////////////////////////////////////////////
    char makeLower(char letter)
    {
     if (((int)letter)>64 && ((int)letter)<91) //checks if its a capital
     {
      (int)letter=(int)letter+32;    //makes it a small letter
      return letter;
     }
     else
     {
     return letter;
     }
    }
    ////////////////////////////////////////////////////
    int YorN()    //ahh, the yes or no question
    {
     char anwser = 'n';
     cin>>anwser;
     if (makeLower(anwser)=='y')  //if the letter, in lowercase, is y
     {
      return 1;     //if it's y, return a 1
     }
     else
     {
      return 0;
     }
    }

  6. #6
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Bart, wouldn't that cause you to creep slowly up the stack? A recursive main() would get costly after a few calls.

  7. #7
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    golfinguy and ninebit had the right idea. Use a big loop to control everything.

    I would never, ever just call main(). Although you could, I wouldn't. b/c like Imperito said, it can get costly.

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >I would never, ever just call main(). Although you could

    No, if you're coding in C++ you couldn't. It's against the law.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to shutdown or restart a PC from a pthread-process
    By mynickmynick in forum C Programming
    Replies: 8
    Last Post: 08-07-2008, 10:11 AM
  2. return to the beginning (restart)
    By hallo007 in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2006, 01:58 PM
  3. restart computer?
    By Frantic- in forum C++ Programming
    Replies: 3
    Last Post: 12-12-2004, 04:28 PM
  4. Can't Restart in MS-DOS Mode
    By Stan100 in forum Tech Board
    Replies: 11
    Last Post: 07-09-2004, 05:08 PM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 03:40 PM

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