How to restart a program without closing it...

This is a discussion on How to restart a program without closing it... within the C++ Programming forums, part of the General Programming Boards category; After the program is finished doing watever its coded to do, how would u restart it? i was told to ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    55

    How to restart a program without closing it...

    After the program is finished doing watever its coded to do, how would u restart it? i was told to use a while loop...i have NO idea how they work....someone plz explain
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    A bit like this...

    Code:
    char yorn;
    do {
        // stuff to be repeated
        cout << "Again?;
        cin >> yorn;
    while ( yorn == 'y' );
    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.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    can you explain wut each part of the code does. its kind of hard to understand it...c++ is all a rush to me..
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  4. #4
    Unregistered
    Guest
    You might want to read the FAQ on cprogramming.com. That should answer most of your questions, and we can take care of anything that the FAQ misses.

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    here

    # include stuff here....

    int main()
    {
    repeatme:
    char closeme;
    cout<<"Hi, I'm a noob!";
    closeme=getch();
    switch(closeme)
    {
    case 'x':
    clrscr();
    break;

    default:
    goto repeatme;
    break;
    }
    return 0;
    }

    You could go this route. I was looking for the same thing in a very large menu type program. People kept telling me to use loops and someone said to just call the program again. I did not want this. Nobody said anything to me about goto statements. Writting batch files for a very long time, it totaly slipped my mind and I never gave goto statements a shot. I felt kinda silly when something so simple worked so well.

    It will print the message, and if you press any key it will repeat. If you press the letter x it will close the program.

    Moreoever, you could put a clear screen command right after the lable "repeatme" so everytime the main function is "repeated" the text is placed at the top of the screen instead of your screen constantly scrolling down everytime you press any button except x. This will repeat the main function endlessly.
    Last edited by Shadow; 10-01-2001 at 07:21 PM.
    The world is waiting. I must leave you now.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    hmm....replying 2 hours after i finally get a good awnser from someone else..with an even EASIER method..lol.....thanks dude...less lines of code to memorize .....1 step closer to the server im gonna build....muahaha
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    wut is the include heading for clrscr(); though...it keeps comin out wit errors
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  8. #8
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    ummmm

    hinrich3@merr.com

    E-mail me, we'll get it straightened out. I use Turbo C++ and with that you can use C AND C++ libraries at the same time. You just merge the different runtime libraries ( including header files from each language )

    For clearing the screen though, I believe it's conio.h

    Actually, I just tested it. It is conio.h.

    #include <conio.h>

    int main()
    {
    clrscr();
    return 0;
    }
    I compiled that really quick, and it works.

    conio.h
    The world is waiting. I must leave you now.

  9. #9
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    actually...

    I am doing something very similar, here it is (also read the end of my post). It gives you a choice of what to go to, including exit. When you are through, the menu comes back and you can choose again....i just need a clear screen statement that works:

    #include <iostream.h>
    #include <conio.h>
    #include <windows.h>
    #include <stdlib.h>
    #include <math.h>
    #include <iomanip.h>
    #include <string>
    #include <fstream.h>

    int main() //Line 10

    {
    int input;
    do
    {
    cout<<"Enter the number of what you want to use"<<flush<<endl;
    cout<<"1. Averager"<<flush<<endl;
    cout<<"2. Multiplier"<<flush<<endl;
    cout<<"3. Divider"<<flush<<endl;
    cout<<"4. Subtracter"<<flush<<endl;
    cout<<"5. Adder"<<flush<<endl;
    cout<<"6. Exit"<<flush<<endl;
    cin>>input;
    switch (input)
    {
    case 1:

    cout<<"This program will get the average of numbers you enter."<<endl;
    float g, x, y, n;
    cout<<"Please enter the scores to be averaged (use a space in between numbers): ";
    cin>>x>>y;
    cout<<"Enter how many scores you entered: ";
    cin>>n;
    g=(x+y)/n;
    cout<<"The average of your numbers is: "<<g<<flush<<endl;
    _getch();
    break;
    case 2:
    int mult(int a, int b);
    cout<<"This program will get the product of two numbers"<<endl;
    int a, b;
    cout<<"Enter the two numbers (with a space in between): "<<endl;
    cin>>a>>b;
    cout<<"The product of your numbers is"<<flush<<mult(a, b)<<endl;
    _getch();
    break;
    case 3:
    int divide(int q, int w);
    int q, w; //Line 61
    cout<<"This program divides two numbers"<<endl;
    cout<<"Enter the numbers (with a space in between): ";
    cin>>q>>w;
    cout<<"The quotient of your two numbers is: "<<flush<<divide(q, w)<<endl;
    _getch();
    break;
    case 4:
    int subtract(int e, int r);
    int e, r; //Line 70
    cout<<"This program subtracts two numbers"<<endl;
    cout<<"Enter the numbers (with a space in between): ";
    cin>>e>>r;
    cout<<"The difference of the numbers is: "<<subtract(e, r)<<flush<<endl;
    _getch();
    break;
    case 5:
    int add(int t, int p);
    int t, p;
    cout<<"This program adds two numbers"<<endl;
    cout<<"Enter the two numbers (with a space in between): ";
    cin>>t>>y;
    cout<<"The sum of the numbers is: "<<flush<<add(t, y)<<endl;
    _getch();
    break;
    case 6:
    return 0;
    break;
    }
    }while(input<6);
    }
    int mult(int a, int b)
    {
    return a*b;
    }
    int divide(int q, int w)
    {
    return q/w;
    }
    int subtract(int e, int r)
    {
    return e-r;
    }
    int add(int t,int p)
    {
    return t+p;
    }

    Now, im using msvc++ 6.0 and conio.h does NOT support clrscr();. It says "undeclared identifier" or something like that.

  10. #10
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    less lines of code to memorize
    don't memorize code...understand it....(and i personally think loops are better than the repeat stuff...but i guess that's opinion...)
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  11. #11
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    here

    After some setbacks ( time restraints ), I finally whipped up a quick program to do what the original poster was asking. Quite possibly this may work for Mr. Leeman.

    If you dont have Turbo C++, download it. It's like 2mb or something, a command line compiler that's very easy to use, and i've had NO problems compiling both C & C++ code.

    With that, compile the following program:

    /*
    A menu program.

    This program will allow the user to execute programs commonly found on
    Windows computers. This program will check to see if the file exists.
    If the file does exist, it will execute it. If it doesn't it will display
    a suitable error message. Since wordpad and sound recorder are commonly found
    on default windows setups, there is an option to try and execute a file that
    obviously does not exist: duherr.exe. This is so we may visualy see that the
    error messages are working correctly.

    With a bit of "tweaking" you could use similar methods to create a permanent
    running log file of operations carried out if you were to create a menu type
    program to run lots of dos programs.
    */

    #include <iostream.h>
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <fstream.h>
    #include <io.h>

    #define menu (cout<<"Choose a selection:\n\n1. Windows Wordpad\n2. Windows sound recorder\n3. A dummy file name that doesn't exist\n\nx = exit\n")

    /*
    The define statement above will, everytime it is called, display this:

    Choose a selection:
    1. Windows Wordpad
    2. Windows sound recorder
    3. A dummy file that doesn't exist

    x = exit
    */

    int main()
    {
    FILE * Tmp;
    /*
    The above prepares for file operations.
    We only need one and since it is a temporary
    file that will be deleted after it's executed,
    we'll call it Tmp.
    */

    /*
    Each instance of Tmp will be used to create a file
    in the background. It will be used to create "tmp.bat"
    so we may execute what we put into the file.

    The contents of tmp.bat:

    @echo off
    if not exist c:\path\program.exe goto error
    c:\path\program.exe goto done

    :error
    echo.
    echo "program being called" does not exist!
    echo You do not have "program being called" on your computer
    echo.
    pause
    goto done

    :done

    */
    repeatmain:
    clrscr();
    char selection;
    menu;
    selection=getch();
    switch(selection)
    {
    case '1':
    Tmp = fopen("tmp.bat", "a");
    fprintf(Tmp, "@echo off\nif not exist c:\\windows\\write.exe goto error\nc:\\windows\\write.exe\ngoto done\n\n:error\necho.\necho Wordpad was not detected!\necho You do not have wordpad on your computer\necho.\npause\ngoto done\n\n:done\n");
    fclose(Tmp);
    system("tmp.bat");
    system("del tmp.bat >NUL");
    goto repeatmain;
    break;

    case '2':
    Tmp = fopen("tmp.bat", "a");
    fprintf(Tmp, "@echo off\nif not exist c:\\windows\\sndrec32.exe goto error\nc:\\windows\\sndrec32.exe\ngoto done\n\n:error\necho.\necho sound recorder was not detected!\necho You do not have sound recorder on your computer\necho.\npause\ngoto done\n\n:done\n");
    fclose(Tmp);
    system("tmp.bat");
    system("del tmp.bat >NUL");
    goto repeatmain;
    break;

    case '3':
    Tmp = fopen("tmp.bat", "a");
    fprintf(Tmp, "@echo off\nif not exist c:\\windows\\duherrr.exe goto error\nc:\\windows\\duherr.exe\ngoto done\n\n:error\necho.\necho duherr was not detected!\necho You do not have duherr on your computer\necho.\npause\ngoto done\n\n:done\n");
    fclose(Tmp);
    system("tmp.bat");
    system("del tmp.bat >NUL");
    goto repeatmain;
    break;

    case 'x':
    clrscr();
    break;

    default:
    goto repeatmain;
    break;
    }
    return 0;
    }

    I might have one too many includes, as i noticed that i had a time.h include in there. I just copied an include list from another program and used it for this example program. It all works just fine, although , like i said it might have an included file or 2 in there you dont need.

    Very simple, gets the job done, and doesn't involve miles of code.
    The world is waiting. I must leave you now.

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    32
    Personally spagetti code is the worst code of all to read! That is to say, code that bounces up and down and all over the place (by using the goto statement). Here are some examples to prove my point... Tell me which one you think is easier to read!


    // Here's the spagetti code.
    int main()
    {

    int Choice;

    Start:
    printf("Welcome to the start!\n");
    goto Choices;

    End:
    printf("Well it was nice seeing you!\n");
    exit(0);

    SomeMidStuff:
    printf("I see you are trying to figure me out!\n");
    goto Choices;

    Choices:
    printf("Wanna exit or see the insides of me? ");
    if( isAlpha(getch()) ) {
    clrscn();
    goto Choices;
    }
    else {
    Choice = getch();
    if( Choice == 1 ) {
    goto SomeMidStuff;
    }
    else {
    goto End;
    }
    }
    }

    // Now for the up-down/down-up code (whatever it's called!)
    int main()
    {
    int Choice;

    printf("Welcome to the start!\n");
    while( Choice != 1 )
    {
    printf("Do you want to exit or see the insides of me? ");
    if( isAlpha(getch()) ) {
    Choice = -1;
    }
    else {
    Choice = getch();
    if( Choice == 0 ) {
    printf("Now you've seen the insides of me!\n");
    }
    if( Choice == 1 ) {
    printf("It was nice seeing you!\n");
    }
    }
    }
    }

    Now you tell me which one is easier to read? (Btw the code may not compile because of the isAlpha() call! I haven't used it in a while so that may or may not work. But it's just to prove my point.

    HTH
    cerion
    Use the code Luke.

  13. #13
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Arrow Hmm

    Both are pretty hard to read without code tags

    code tag:
    [ C O D E ] "the actual code" [ / C O D E ] (without spacing)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  14. #14
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    Cerion

    With menu stuff in cC or C++ the only way to concieve something like that is with goto statements. If you think differently I'll send ya the code "sometime" for a menu i've been working on ( got side-tracked tho, it's that real life syndrome ). Last time it was checked I believe it was up to over 2000 lines.
    The world is waiting. I must leave you now.

  15. #15
    Registered User
    Join Date
    Sep 2001
    Posts
    32
    Lol, sorry not laughing at you but your statement. Over the years when I was in school I had been forced to do tones of menu based programs. TONES. Both large and small, the largest being the final grade 12 project and I believe it was something like 4000 lines long. I never once used the goto statement. Instead I used a loop and then used functions that were called based on choice from the menu. The functions doing the required actions. The menu section of the code was always the smallest, however, and the actually functions being the largest.

    I used to script for mIRC (yes the chatting program) and that was spagetti coding until they added loop statements. Then once they added the loop statements then I stopped using the goto statements because it became easier for me to not only read my scripts but also debug my scripts.

    So in my honest opinion, no goto statements are not the only conceivable way of accomplishing a menu based program. Not even close, and infact it makes it harder to read. However, if you do not know functions yet I suggest you learn them because almost every tutorial or example of code you'll find on the net doesn't contain goto statements. Infact this is the first time I, personally, have ever seen C code that uses goto statements.

    HTH
    cerion
    Use the code Luke.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. exiting and closing a program
    By major_small in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2003, 08:31 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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