Thread: returning to a spot in the code

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

    returning to a spot in the code

    I jump into a switch statement to handle all my cases. When the case is chosen and the action is carried out, how can i return without just calling the program again ( ex: system("um.exe"); ).

    It may seem kind of silly since it's much easier to recall the program, but say at the very beginning of the program i created a file and wrote to it the following phrase: "You ran the following:" , then each case that was carried out would write it's name down in that file. This outside text file would be closed when the program termintated.

    #include <stdio.h>
    #include <conio.h>

    int main()
    {
    char select;
    clrscr();
    printf("Choose\n\n1. Msg 1\n2. Msg 2\n");
    select=getch();
    switch(select)
    {
    case '1':
    printf("press a key...");
    getch();
    /* what goes here, besides "name of this program".exe */ ;
    break;

    case '2':
    printf("press a key...");
    getch();
    .......
    break;

    default:
    .......
    break;

    case 'x':
    clrscr();
    return 0;
    }
    }
    The world is waiting. I must leave you now.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    At the start of main
    &nbsp; FILE *fp = fopen( "file.log", "w" );

    In each case
    &nbsp; char *prog = "foo.exe";
    &nbsp; fprintf( fp, "Running prog: %s\n", prog );
    &nbsp; system( prog );

    At the end of main
    &nbsp; fclose( fp );

    Any use???
    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.

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

    hmm

    At the start of main
    FILE *fp = fopen( "file.log", "w" );

    In each case
    char *prog = "foo.exe";
    fprintf( fp, "Running prog: %s\n", prog );
    system( prog );

    At the end of main
    fclose( fp );

    ...This would open a file and write everything that's done while this custom program is running. I would open thist newly generated file and see "program so and so was ran", and see a nice list of everything that was done. But, system( prog ) REopens the program. I want to create on single entry in that log for the entire time the program is running.

    example ( of the log )

    Date:
    The program ran:
    case 1 execution ( notepad or something... )

    the very next time i run a different case the program was recalled so....

    Date:
    The program ran:
    case 3 execution ( ..i-dunno solitare )

    Instead of 2, i want 1:

    Date:
    The program ran:
    notepad
    solitare

    I want to know how to stay in the program after executing a case "without" recalling the program. I haven't been able to figure this out. There has to be a way...
    The world is waiting. I must leave you now.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I want to know how to stay in the program after executing a case "without" recalling the program.
    But system("prog") does this anyway, so I can't see what the problem is.

    The program calling system() doesn't die, it just goes to sleep until "prog" exits, then the calling program resumes execution at the next instruction (just like any other function call).
    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.

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

    I figured it out ! !

    Excuse the long reply, but i got it !

    The way you suggested is exactly what i was doing, which wasn't working.

    This is going to be a mile long post, so sorry...

    This is how i was doing it and basically what you suggested:

    #include <iostream.h>
    #include <stdio.h>
    #include <conio.h>
    #include <time.h>
    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    char select;
    clrscr();
    time_t hold_time;
    hold_time=time(NULL);

    FILE * File;
    File = fopen("um.txt", "a");

    fprintf(File, ctime(&hold_time));
    fprintf(File, "Menu ran:\n");

    printf("Choose\n\n1. Msg 1\n2. Msg 2\n");
    select=getch();
    switch(select)
    {
    case '1':
    printf("press a key...");
    fprintf(File, "Case 1\n");
    getch();
    system("um.exe");
    break;

    case '2':
    printf("press a key...");
    fprintf(File, "Case 2\n");
    getch();
    system("um.exe");
    break;

    default:
    system("um.exe");
    break;

    case 'x':
    clrscr();
    fprintf(File, "\n");
    fclose(File);
    return 0;
    }
    }

    The results in um.txt :

    Sat Sep 22 06:18:58 2001
    Menu ran:
    Case 2
    Sat Sep 22 06:18:57 2001
    Menu ran:
    Case 2
    Sat Sep 22 06:18:56 2001
    Menu ran:
    Case 1
    Sat Sep 22 06:18:56 2001
    Menu ran:
    Case 1
    Sat Sep 22 06:18:55 2001
    Menu ran:
    Case 2
    Sat Sep 22 06:18:51 2001
    Menu ran:
    Case 1
    Sat Sep 22 06:19:08 2001
    Menu ran:

    Sat Sep 22 06:19:07 2001
    Menu ran:
    Case 2
    Sat Sep 22 06:19:06 2001
    Menu ran:
    Case 1

    ...... That's kinda messy, dont ya think ?

    repeat:
    clrscr();
    printf("Choose\n\n1. Msg 1\n2. Msg 2\n");
    select=getch();
    switch(select)

    Put a "repeat" label right before clearing the screen to repeat the menu again ( still keepin the log open, without closing and re-opening it...also keeping one date and time for this menu.exe execution )

    after every case simply add " goto repeat; " it jumps to the top not terminating, keeping the same date and time of startup ( and you can even slip a closing time during the exit routine.

    This results in a much cleaner log file....

    Sat Sep 22 06:17:23 2001
    Menu ran:
    Case 1
    Case 1
    Case 2
    Case 2
    Case 2
    Case 2
    Case 2
    Case 1
    Case 1
    Case 1

    Sat Sep 22 06:17:40 2001
    Menu ran:
    Case 1
    Case 1
    Case 1
    Case 1
    Case 1
    Case 2
    Case 2

    Thanks for the help, but i figured it out !
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  2. Returning error code in linked list, typecast?
    By difficult.name in forum C Programming
    Replies: 5
    Last Post: 11-26-2004, 10:00 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Can anyone spot the error in my code
    By korbitz in forum C Programming
    Replies: 2
    Last Post: 01-05-2002, 02:05 AM