Restarting a program

This is a discussion on Restarting a program within the C Programming forums, part of the General Programming Boards category; Hello, Is there anyway of going back to the beginning of a program without using a 'goto'? I have a ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    21

    Restarting a program

    Hello,

    Is there anyway of going back to the beginning of a program without using a 'goto'? I have a main menu at the beginning of my program which i want it to go back 2 several times in the program if the user inputs the wrong information.

    If there is no other way than using gotos, is there anyway to get a goto to a main program work from inside a function?

    Thanx for any help,
    Jason

  2. #2
    .........
    Join Date
    Nov 2002
    Posts
    303
    Hmm for that I would think a goto would not be the best choice I agree.
    Something like...
    Code:
    START
      while (something)
        mainmenu() // call this
    END
    
    mainmenu()
    { 
       //if something goes wrong let it return
       // and make that something Not true
       // or repeat mainmenu()
        func1()
        func2()
        func2()
    }
    
    func1()
    { 
         //do stuff, if something goes wrong, 
         // let it return or repeat func1
         // depending on what you want it to 
         // do
    }
    func2()
    {
         // same as func2
    }
    
    func3()
    {
        // same as func3
    }
    Just an idea, if you wanted to actually use a goto, I would think it would be better put to use for something like
    Code:
    for // first loop
     for // second loop
      for // third loop
        if error 
          // goto the first loop or somewhere   
          // else
    In a situation like that a goto would be better I think because multiple breaks can get messy. But for something like menu's you do not need them, as I think they might make the program harder to read. Goodluck, just write it so it makes sense to you. If it makes sense to you there is a good chance(not gauranteed) that it makes more sense to others. Post what you have so far.

    Also forgot to add, you can simply call mainmenu() when you have an error like.
    Code:
    func1()
    {
           if (error)
              mainmenu()
    }
    When I first started programming five months ago I found I had this exact same problem, but now it seems easier. The more you practice the easier it will get. Just try to make things as simple as possible, goodluck.
    Last edited by SourceCode; 05-07-2003 at 05:42 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To continue on the above, if you truely want it to "start over", you'll want to have your functions take a parameter that lets them know that they're starting over. This would be used to basicly set a flag to cause them to cleanup and reinitialize all variables. Consider:
    Code:
    void fun( some args, unsigned char init )
    {
        vars declarations;
    
        if( init )
        {
            declarations = 0;
            freeing_allocated_memory( );
            setting_pointers_to_null( etc );
        }
    
        ...rest of function...
    }
    If you truely want it to act like it's started over, then you'd want to do something like I've described.

    As per the previous post, you want all function calls prior to this to have terminated, thus freeing their stack space.

    Otherwise, if your functions just call their "menu" function over again, you'll have recursion, and eventually you'll run out of stack space.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    8
    Sounds to me like you need multiple do-while loops.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    21
    Hello,

    Hav read your posts but cant think of a way to use a loop instead of a 'goto' statement. Im trying to write a program for a airline booking system. There is an array to represent the seats which are 0 is empty and 1 if not. I have started off the program with a menu so the user can have the option to pick a specific seat number or let the program find the next free seat automatically.

    They then have the option to book the seat, if yes they enter therir deatials etc if no then i want the program to return to the main menu. I have put a section of my code below to show you what i mean. I can send the whole code if it helps.

    Thanx 4 all this help
    Jason

    main ()
    {
    int seats[]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    Beginning:
    int book_confirm, option = 0, option2=0;

    clrscr();
    mainscreen();
    scanf("%d", &option);
    if ((option ==0) || (option > 3))
    {
    error2();
    mainscreen();
    scanf("%d", &option);
    }
    clrscr();

    switch (option)
    {
    case 1 : servicechoice();
    scanf("%d", &option2);
    clrscr();
    switch (option2)
    {
    case 1: for (seatno=1; seatno<=5; seatno++)
    {
    if ((seatno==5) && (seats[5]==1))
    {
    caseprt1();
    if (nxt_compart_confirm==1)
    {
    for (seatno=5; seatno<=10; seatno++)
    {
    if (seats[seatno]==0)
    {
    seatavailable();
    scanf("%d", &book_confirm);
    if (book_confirm == 1)
    {
    seats[seatno]=1;
    puts("\nPlease enter your name: ");
    scanf("%s", &passenger_names[seatno]);
    ticket();
    getch();
    goto Beginning;
    }
    else
    goto Beginning;

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Your braces don't add up but have a look at this and see if you can fit your code around it:
    Code:
    int main ()
    {
      int seats[]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
      
      do
      {
        int book_confirm, option = 0, option2=0;
    
        /* ... blah blah blah ... */
    
        switch (option)
        {
        case 1: 
          /* ... blah ..*/
          switch (option2)
          {
          case 1:
            /* ... more blah ... */
            break;
          case 2:
            /* ... surely ... */
            break
          default:
            /* error handling */
            break;
          }
          break;
        case 2:
          /* ... yet more blah ... */
          break;
        default:
          /* error handling */
          break;
        }
      } while(true);
    
      return 0;
    }
    This will keep going forever so you might want to change the while(true) to something that actually can drop through to the end of the program.

    (btw - notice how much easier it is to read with code tags and suitable indentation!!! )
    Last edited by DavT; 05-08-2003 at 05:51 AM.
    DavT
    -----------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  3. Newbie problem in restarting program
    By cherub in forum C Programming
    Replies: 8
    Last Post: 02-12-2005, 09:05 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