Menu() function, choose with Switch() and return to Menu()

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 18
Reputation: kacete is an unknown quantity at this point 
Solved Threads: 0
kacete's Avatar
kacete kacete is offline Offline
Newbie Poster

Menu() function, choose with Switch() and return to Menu()

 
0
  #1
Dec 26th, 2008
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:
  1. int Menu()
  2. {
  3. int i;
  4. printf("MENU\n\n");
  5. printf("1 - ...\n");
  6. printf("2 - ...\n");
  7. printf("3 - ...\n");
  8. printf("4 - ...\n");
  9. printf("5 - ...\n\n");
  10. printf("Choose: ");
  11. scanf("%d",&i);
  12. printf("\n\n");
  13. return i;
  14. }

And I have been calling it using this on main():

  1. void main()
  2. {
  3. int op;
  4. op = Menu();
  5. switch(op)
  6. {
  7. case 1:
  8. funct1();
  9. break;
  10. case 2:
  11. funct2();
  12. break;
  13. case 3:
  14. funct3();
  15. break;
  16. case 4:
  17. funct4();
  18. break;
  19. case 5:
  20. funct5();
  21. break;
  22. default:
  23. printf("ERROR: Invalid.\n\n");
  24. break;
  25. }
  26. }

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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Menu() function, choose with Switch() and return to Menu()

 
0
  #2
Dec 26th, 2008
Put Menu() inside a loop.
void main() is a no no. Should always return an int.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: kacete is an unknown quantity at this point 
Solved Threads: 0
kacete's Avatar
kacete kacete is offline Offline
Newbie Poster

Re: Menu() function, choose with Switch() and return to Menu()

 
0
  #3
Dec 26th, 2008
Originally Posted by Aia View Post
Put Menu() inside a loop.
void main() is a no no. Should always return an int.
Like
  1. int main()
  2. {
  3. while(program not ended)
  4. {
  5. menu()....
  6. switch....
  7. }
  8. return 0;
  9. }
?

So
  1. int main()
  2. {
  3. return 0;
  4. }
is preferable to
  1. void main()
  2. {
  3. }
why?
Last edited by kacete; Dec 26th, 2008 at 11:33 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,868
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Menu() function, choose with Switch() and return to Menu()

 
1
  #4
Dec 26th, 2008
>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:
  1. /* When expecting no command line arguments */
  2. int main ( void )
  3. {
  4. /* ... */
  5. }
  6.  
  7. /* When expecting command line arguments */
  8. int main ( int argc, char *argv[] )
  9. {
  10. /* ... */
  11. }
Further, there are only three predictable return values from main. Deviating from them means your code may not behave as expected on termination:
  1. /* Return a success code */
  2. return 0;
  3.  
  4. /* EXIT_SUCCESS defined in <stdlib.h>, return a success code */
  5. return EXIT_SUCCESS;
  6.  
  7. /* EXIT_FAILURE defined in <stdlib.h>, return a failure code */
  8. return EXIT_FAILURE;
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.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: kacete is an unknown quantity at this point 
Solved Threads: 0
kacete's Avatar
kacete kacete is offline Offline
Newbie Poster

Re: Menu() function, choose with Switch() and return to Menu()

 
0
  #5
Dec 26th, 2008
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:

  1. int main()
  2. {
  3. int end = 0;
  4. while(end!=1)
  5. {
  6. int op;
  7. system("CLS");
  8. op = Menu();
  9. switch(op)
  10. {
  11. case 1:
  12. funct1();
  13. break;
  14. case 2:
  15. funct2();
  16. break;
  17. case 3:
  18. funct3();
  19. break;
  20. case 4:
  21. funct4();
  22. break;
  23. case 5: // This is the option to quit the program
  24. end = 1;
  25. break;
  26. default:
  27. printf("ERRO: Opcao invalida.\n\n");
  28. system("PAUSE");
  29. break;
  30. }
  31. }
  32. return 0;
  33. }

Thank you all!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Menu() function, choose with Switch() and return to Menu()

 
0
  #6
Dec 26th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: kacete is an unknown quantity at this point 
Solved Threads: 0
kacete's Avatar
kacete kacete is offline Offline
Newbie Poster

Re: Menu() function, choose with Switch() and return to Menu()

 
0
  #7
Dec 26th, 2008
Originally Posted by Aia View Post
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.
Yes, it is meant for Windows OS only, as it's a good workaround if portability is not an issue. I didn't get the different process problem, when i run the program it all happens in the same window.

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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 90
Reputation: ajay.krish123 is an unknown quantity at this point 
Solved Threads: 8
ajay.krish123 ajay.krish123 is offline Offline
Junior Poster in Training

Re: Menu() function, choose with Switch() and return to Menu()

 
0
  #8
Dec 26th, 2008
Thanking you guys,i didnt knew that concept.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,868
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Menu() function, choose with Switch() and return to Menu()

 
1
  #9
Dec 26th, 2008
>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:
  1. if ( scanf ( "%d", &i ) != 1 ) {
  2. /* Handle the error */
  3. }
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:
  1. for ( ; ; ) {
  2. /* Display options */
  3.  
  4. if ( scanf ( "%d", &i ) == 1 )
  5. break;
  6. else {
  7. int ch;
  8.  
  9. clearerr ( stdin );
  10.  
  11. do
  12. ch = getchar();
  13. while ( ch != '\n' && ch != EOF );
  14.  
  15. clearerr ( stdin );
  16. }
  17. }
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: kacete is an unknown quantity at this point 
Solved Threads: 0
kacete's Avatar
kacete kacete is offline Offline
Newbie Poster

Re: Menu() function, choose with Switch() and return to Menu()

 
0
  #10
Dec 26th, 2008
I didn't know scanf() could return values, I guess that makes the testing easier! So for that test code to work it would require a no parameter for( ; ; )?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1963 | Replies: 15
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC