| | |
Menu() function, choose with Switch() and return to Menu()
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Hello everyone, I've been using this website and it has helped me with it's hundred of solved cases, but I couldn't find one appropriate for this one.
I have created a Menu() function that looks like this:
And I have been calling it using this on main():
It works great, no problems at all. I just wanted to know how to, if possible, return to the Menu() after funct1() has ended, for example.
Thank you!
I have created a Menu() function that looks like this:
C Syntax (Toggle Plain Text)
int Menu() { int i; printf("MENU\n\n"); printf("1 - ...\n"); printf("2 - ...\n"); printf("3 - ...\n"); printf("4 - ...\n"); printf("5 - ...\n\n"); printf("Choose: "); scanf("%d",&i); printf("\n\n"); return i; }
And I have been calling it using this on main():
C Syntax (Toggle Plain Text)
void main() { int op; op = Menu(); switch(op) { case 1: funct1(); break; case 2: funct2(); break; case 3: funct3(); break; case 4: funct4(); break; case 5: funct5(); break; default: printf("ERROR: Invalid.\n\n"); break; } }
It works great, no problems at all. I just wanted to know how to, if possible, return to the Menu() after funct1() has ended, for example.
Thank you!
•
•
•
•
Put Menu() inside a loop.
void main()is a no no. Should always return an int.
C Syntax (Toggle Plain Text)
int main() { while(program not ended) { menu().... switch.... } return 0; }
So
C Syntax (Toggle Plain Text)
int main() { return 0; }
C Syntax (Toggle Plain Text)
void main() { }
Last edited by kacete; Dec 26th, 2008 at 11:33 am.
>why?
Because when you use void main, your code is no longer guaranteed to compile or run. The C standard specifies two definitions of main, and you should use one of those unless you have a very good reason not to:
Further, there are only three predictable return values from main. Deviating from them means your code may not behave as expected on termination:
The point of these rules is to make your code portable to every compiler on every machine that supports standard C. If you don't follow them, you lose that flexibility to the point where your code is only portable to the exact version of the compiler and machine you initially wrote your code on.
Because when you use void main, your code is no longer guaranteed to compile or run. The C standard specifies two definitions of main, and you should use one of those unless you have a very good reason not to:
c Syntax (Toggle Plain Text)
/* When expecting no command line arguments */ int main ( void ) { /* ... */ } /* When expecting command line arguments */ int main ( int argc, char *argv[] ) { /* ... */ }
c Syntax (Toggle Plain Text)
/* Return a success code */ return 0; /* EXIT_SUCCESS defined in <stdlib.h>, return a success code */ return EXIT_SUCCESS; /* EXIT_FAILURE defined in <stdlib.h>, return a failure code */ return EXIT_FAILURE;
New members chased away this month: 5
Well, that gives me one good reason to change my code and start using int main() from now on!
Thank you guys!
Anyway, the main problem is resolved, and here's the solution for future viewers:
Thank you all!
Thank you guys!Anyway, the main problem is resolved, and here's the solution for future viewers:
C Syntax (Toggle Plain Text)
int main() { int end = 0; while(end!=1) { int op; system("CLS"); op = Menu(); switch(op) { case 1: funct1(); break; case 2: funct2(); break; case 3: funct3(); break; case 4: funct4(); break; case 5: // This is the option to quit the program end = 1; break; default: printf("ERRO: Opcao invalida.\n\n"); system("PAUSE"); break; } } return 0; }
Thank you all!
The use of
[edited]
The use of
system("CLS"); and system("PAUSE"); tight that code to operating systems that have those calls. Furthermore, system() will open a different process.[edited]
The use of
scanf("%d",&i); opens the possibility to a forever loop in Menu() if the user enters something different than an integer. Last edited by Aia; Dec 26th, 2008 at 12:21 pm.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
•
•
•
•
The use ofsystem("CLS");andsystem("PAUSE");tight that code to operating systems that have those calls. Furthermore, system() will open a different process.
In response to the edit: Yes, it is in fact true, and I actually did it accidentally, but It's not meant to be a too complex program.
Or is there some simple if test function to check if the entered symbol is an int? Last edited by kacete; Dec 26th, 2008 at 12:48 pm.
>Or is there some simple if test function to check if the entered symbol is an int?
scanf returns the number of successful conversions. The test itself is as simple as this:
Handling the error is harder because you need to clear out the bogus input and make sure the state of the stream is prepared for another input request. Usually this amounts to flushing the stream entirely and clearing the error state before jumping back to the input request:
scanf returns the number of successful conversions. The test itself is as simple as this:
c Syntax (Toggle Plain Text)
if ( scanf ( "%d", &i ) != 1 ) { /* Handle the error */ }
c Syntax (Toggle Plain Text)
for ( ; ; ) { /* Display options */ if ( scanf ( "%d", &i ) == 1 ) break; else { int ch; clearerr ( stdin ); do ch = getchar(); while ( ch != '\n' && ch != EOF ); clearerr ( stdin ); } }
New members chased away this month: 5
![]() |
Similar Threads
- need a little help...problemis in highlighted area (C++)
- Help, I don't think I'm asking for much. This is a simple java program. (Java)
- Help with hangman tutorial. (C++)
- Menu help please - Newbie (C)
- C programming - need some help (C)
- help to debug my (C++)
- help to debug my code (C++)
- Linked Lists stresses (C++)
- how to call this function...using menu..! (C++)
- The Calculator (C++)
Other Threads in the C Forum
- Previous Thread: need help
- Next Thread: passing matrix by ref
Views: 1963 | Replies: 15
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student systemcall test testautomation unix user variable visualstudio voidmain() wab win32 win32api windows.h






