Jumping between functions

This is a discussion on Jumping between functions within the C Programming forums, part of the General Programming Boards category; Newbie here! I was wondering if it is possible to jump back to a previous function in a program and ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    26

    Jumping between functions

    Newbie here! I was wondering if it is possible to jump back to a previous function in a program and if it is how would one go about doing it?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Jump back as in aborting your currently executing function and return to the previous or as in calling the previous called function from the current?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think what you'd need for that might be in this header: <setjmp.h>

    (You'll probably need to find some resources that teaches their use. I know Thinking in C++ which is available for downloading demonstrates error handling using the jumps in the second volume.)

    However, this sounds like a question where you'd better describe the problem you are trying to solve because there might be massive amounts of spaghetti ahead.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    What i want is to be able to jump back to the main menu in main from the registration function. I thought return would allow me to do it, but i was wrong.
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    #define BUF_SIZE 128
    
    int Registration(int* user);
    int Login(int* user); 
     
    int main (int a)
    
    {
    	
    	int user;	
    	int log;
    	int reg;
    	char ans;
    		
    		printf ("Please select from the following...");
    	
    		printf ("\n1)[R]egister");
    	
    		printf ("\n2)[L]ogin");
    	
    		printf ("\n3)[U]nregister");
    	
    		printf ("\n");
    	
    		scanf  ("%c", &ans);
    	
    	{	 	 
    			if (ans == 'l' || ans == 'L' || ans == '1')
    				Login(&user);
    			
    			else if (ans == 'r' || ans == 'R' || ans == '2')
    				Registration(&user);
    	
    			else 
    				printf("Invalid!\n");
    	}
    }
    		
    
    
    int Registration(int* user)
    
    {
    
    	FILE *db;
    
    	db = fopen("text.txt","a+");
    
    		printf("Please enter registration user name here: ");
    
    		fscanf (stdin, "%s", &user);
    
    		fprintf(db,"%s \n",&user );
    
    		fclose(db);
    
    		printf("Registration Accepted!");
    
    		getchar();
    		
    		return 0;
    
    		
    	
    }
    
    int Login(int* user)
    {
    	
    	printf ("Enter Login name:");
    	scanf  ("%d", &user);
    
    }

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    How about something like:
    Code:
    int quit = 0;
    while(quit == 0)
    {
        DisplayMenu();
        DoOtherStuff();
        if( ans == 'q') quit = 1
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Nexus-ZERO View Post
    What i want is to be able to jump back to the main menu in main from the registration function. I thought return would allow me to do it, but i was wrong.
    Of course return takes you back to main. But that's the key--it takes you back, that is, to where you called the function from in the first place. There's just nothing left to do there, anymore, since you set main up to only display the menu once and then stop. If you want to see the menu again, then your main program must continue to run in some sort of loop.

    Code:
    int main (int a)
    There are a couple of ways to specify main, but this isn't one of them. You can do
    Code:
    int main(void)
    or
    Code:
    int main(int argc, char *argv[])
    Since you nowhere use a, I would suggest the first.

    Your login function claims to return an int, but does not do so, causing your compiler to have to magically come up with one, somehow. You should either save the compiler the bother by returning something, or if you don't want to return anything, specify a return type of void.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    int main( int argc, char *argv[] )
    {
          int user;	  
          int log;
          int reg;
          char ans=0;
    	
          while( ans != 'Q' || ans != 'q' )
          {
              printf ("Please select from the following...");
              printf ("\n1)[R]egister");
              printf ("\n2)[L]ogin");
              printf ("\n3)[u]nregister");
              printf ("\n4)[Q]uit");
              printf ("\n");
              scanf  ("&#37;c", &ans);
        
              if (ans == 'l' || ans == 'L' || ans == '1')
    	         Login(&user);
              else if (ans == 'r' || ans == 'R' || ans == '2')
    	         Registration(&user);
              else if( ans != 'Q' || ans != 'q' )
                             return 0;
              else
    	         printf("Invalid!\n");
          }
          return 0;
    }
    Well the way you where calling the Login function looks not right to me. What are you trying to do. And your need to indented properly. Check with Preview button before you post. The above code will explain the way you want to get back to the main function using return.

    ssharish

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No <setjmp.h> stuff is needed. Just a common menu being returned to from a function.

    As noted, the menu display just needs to be:

    in a loop that repeats until you want to stop or quit.

    Return is the standard way to go back to the calling menu, from your function.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ssharish2005 View Post
    Code:
    int main( int argc, char *argv[] )
    {
          int user;	  
          int log;
          int reg;
          char ans=0;
    	
          while( ans != 'Q' || ans != 'q' )
          {
              ... Snipped for brevity
          }
          return 0;
    }
    ssharish
    Why not use
    Code:
    int main( int argc, char *argv[] )
    {
          int user;	  
          int log;
          int reg;
          char ans=0;
    	
          do
          {
              ... 
          } while ( ans != 'Q' || ans != 'q' )
          return 0;
    }
    It makes little difference, but as a "custom", do - while should be used for "do things at least once", and while loops for "do zero or more times".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 06:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 12:50 PM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 02:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 06:39 AM

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