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?
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?
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.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
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); }
How about something like:
Code:int quit = 0; while(quit == 0) { DisplayMenu(); DoOtherStuff(); if( ans == 'q') quit = 1 }
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.
There are a couple of ways to specify main, but this isn't one of them. You can doCode:int main (int a)
orCode:int main(void)
Since you nowhere use a, I would suggest the first.Code:int main(int argc, char *argv[])
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.
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.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 ("%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; }
ssharish
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.
Why not useIt 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".Code:int main( int argc, char *argv[] ) { int user; int log; int reg; char ans=0; do { ... } while ( ans != 'Q' || ans != 'q' ) return 0; }
--
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.