Thread: how to restart

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    13
    Rep Power
    0

    how to restart


    is there any way for me to let the user decide if they would like to restart a program...
    if i tell them to hit 0 to quit, and 1 to restart, is there any way to restart the program from the start in a ms-dos based application in c++?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Of course, part of the complete restart is to reinitialize everything.

    This thought just occurred to me. I've never tried it, but it seems that it should work.

    There is a family of C functions called exec* (treat the * as a command line wildcard). What they do is that they load another program on top of the current program completely replacing the current program. It may seem like a strange thing to want to do in DOS, but believe me it is very handy in UNIX (which is where C came from to begin with, after all).

    But what I've never tried is to have a program exec*() itself. I would think that what would happen is that it would reload the program over itself and start up as if directly from the command line.

    An added benefit is that when you call exec*, you also give the program a list of arguments, again as if passed to it from the command line. So if you need to change the parameters for whatever reason, you could do it.

    Does that sound like what you're looking for?

    PS
    Yes, C++ has the exec* function family, too.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    13
    Rep Power
    0
    well... this is what i was thinking about but i dont know how to get out of the program and were to put anything ealse for a r = 1 value... it works good if you put 2 in... or any other number

    im not sure i understand your theory i have no previous exp. with c and very little exp with c++.

    it also seems like there could be an if statement?

    Code:
    main()
    {
    int r;
    do{
      cout << "Hello World!" << endl;
      cout << "please press 2 if you would like to restart program, or 1 to end"<<endl;
      cin>> r;
       }while (r=2);
       return 0;
    
    }
    Last edited by need_to_no; October 6th, 2003 at 08:46 PM.
  6. #4
  7. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1511
    (r=2) is going to test true every time. You want (r==2)
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222

    As you were!


    As you were, need_to_no. I misunderstood your question.

    Though that was an interesting idea you gave me.


    And f'lar is quite right. (r=2) is an assignment expression which assigns the value of 2 to the variable r. The value it evaluates to is the value assigned -- 2 in this case. Since a value of zero is false and any non-zero is true, that expression will always be true.

    You must learn to be very careful about distinguishing between the assignment operator (=) and the equality relational operator (==). We've all been bit that that error, so we know to watch out for it. Now you also know.
    Last edited by dwise1_aol; October 6th, 2003 at 09:37 PM.
  10. #6
  11. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95

    Re: As you were!


    Originally posted by dwise1_aol
    As you were, need_to_no. I misunderstood your question.

    Though that was an interesting idea you gave me.
    funny you mentioned i was messing around with same idea last night :cool:
    Code:
    int main(int argc, char **argv)
    {
            int x = 0;
            char    arg[24];
            
            if(argc == 2){
                    printf("I am version %d\n", x = atoi(argv[1])) ;
                    snprintf(arg, 23, "%d", ++x);
                    if( x < 20)
                            execl("./a.out", "a.out", arg, (char *)0);
                    else
                            return 1;
            } else{
                    snprintf(arg, 23, "%d", ++x);
                    execl("./a.out", "a.out", arg, (char *)0);
            }
    
            return 0;
    }
    i was thinking about a program that would contain its source code in a buffer, and change part of it each time and recompile itself. fun stuff :D
  12. #7
  13. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    Pretty kewl example infamous... ;)
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    13
    Rep Power
    0
    wow that was such a simple mistake:( i cant beleave i didnt think of that:(

IMN logo majestic logo threadwatch logo seochat tools logo