Problem About Trying to Return to Main.

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

Join Date: Feb 2008
Posts: 4
Reputation: Theanthropical is an unknown quantity at this point 
Solved Threads: 0
Theanthropical Theanthropical is offline Offline
Newbie Poster

Problem About Trying to Return to Main.

 
0
  #1
Feb 15th, 2008
hi,I have a problem here.

  1. #include "stdafx.h"
  2.  
  3. int main()
  4. {
  5. int speed, dist, time, force, mass, masss, accel, velo, moment;
  6. int choice, choose;
  7. char pilih, pick;
  8.  
  9. do{
  10. printf("\t\t\tWelcome to Physics (Force and Motion)\n");
  11. printf("\t\t***************************************************\n");
  12. printf("\t\t\tCHOOSE SECTION\n\n");
  13.  
  14. printf("\t1. FORCE\n");
  15. printf("\t2. SPEED\n");
  16. printf("\t3. MOMENTUM\n");
  17. printf("\t4. END\n\n");
  18. printf("Choice: ");
  19. scanf("%d", &choice);
  20.  
  21. switch(choice){
  22.  
  23. case 1:
  24. do{
  25. printf("\n\t\tCHOOSE WHICH SECTION\n");
  26. printf("\t1. Info\n");
  27. printf("\t2. Calculation\n");
  28. printf("\t3. End\n");
  29. printf("Choice: ");
  30. scanf("%s", &pilih);
  31.  
  32.  
  33. switch(pilih){
  34.  
  35. case '1':
  36. printf("Force = Mass * Acceleration\n");
  37. printf("The unit is Newton\n");
  38. break;
  39.  
  40. case '2':
  41. printf("Force = Mass * Acceleration\n\n");
  42. printf("Input Mass in kilogram: ");
  43. scanf("%d", &mass);
  44. printf("Input Acceleration in meter per second second: ");
  45. scanf("%d", &accel);
  46. force = mass * accel;
  47. printf("Force is %d\n", force);
  48. break;
  49.  
  50. case '3':
  51. printf("End\n");
  52. break;
  53.  
  54. default:
  55. printf("Invalid section\n");
  56. }break;
  57. }while(pilih !=1);
  58.  
  59.  
  60.  
  61. case 2:
  62. do{
  63. printf("\t\t\tCHOOSE WHICH SECTION\n");
  64. printf("1.Info\n");
  65. printf("2.Calculation\n");
  66. printf("3.End\n");
  67. printf("Choice: ");
  68. scanf("%d", &choose);
  69.  
  70. switch(choose){
  71.  
  72. case 1:
  73. printf("Speed = Distance Taken / Time\n");
  74. printf("The unit in meter per second\n");
  75. break;
  76.  
  77. case 2:
  78. printf("Speed = Distance Taken / Time\n");
  79. printf("Input distance in meter:");
  80. scanf("%d", &dist);
  81. printf("Input time in second:");
  82. scanf("%d", &time);
  83. speed=dist/time;
  84. printf("Speed is %d\n", speed);
  85. break;
  86.  
  87. case 3:
  88. printf("End\n");
  89. break;
  90.  
  91. default:
  92. printf("Invalid section\n");
  93. }
  94. }while(choose !=2);
  95.  
  96.  
  97.  
  98. case 3:
  99. do{
  100. printf("\t\t\tCHOOSE WHICH SECTION\n");
  101. printf("1.Info\n");
  102. printf("2.Calculation\n");
  103. printf("3.End\n");
  104. printf("Choice: ");
  105. scanf("%s", &pick);
  106.  
  107. switch(pick){
  108.  
  109. case '1':
  110. printf("Momentum = Mass * Velocity\n");
  111. printf("The unit in kilogram meter per second\n");
  112. break;
  113.  
  114. case '2':
  115. printf("Momentum = Mass * Velocity\n");
  116. printf("Input Mass in kilogram:");
  117. scanf("%d", &masss);
  118. printf("Input Velocity in meter per second:");
  119. scanf("%d", &velo);
  120. moment=masss*velo;
  121. printf("Momentum is %d", moment);
  122. break;
  123.  
  124. case '3':
  125. printf("End\n");
  126. break;
  127.  
  128. default:
  129. printf("Invalid section\n");
  130. }
  131. }while(pick !=3);
  132.  
  133.  
  134. case 4:
  135. printf("Thank you for using this program\n\n");
  136. printf("This program are made by Jakuzzi group\n\n");
  137. printf("The creator are:\n\n");
  138. printf("\t1.MOHAMAD JEFFRI BIN LEE MOHD. RAMLI\n");
  139. printf("\t2.AMAR BIN MOHD. MARITI\n");
  140. printf("\t3.WAN NORAIMI BINTI ZAINAL ABIDIN\n");
  141. printf("\t4.MUHAMMAD DANIAL BIN ISMAIL\n\n");
  142. printf("\tFrom Mecha 2 Sem 2 2008\n");
  143. printf("\tGERMAN-MALAYSIAN INSTITUTE\n");
  144. break;
  145.  
  146. default:
  147. printf("Invalid selection\n");
  148.  
  149. }
  150. }while(choice !=4);
  151.  
  152. return 0;
  153. }

well, as you can see, this is a very simple physics program we made for a simple assignment.
the problem is we dont know how to make it return to the main menu from the line 51,83, and 125.
we're new in this field and any help will be greatly appreciated. =]
Last edited by WolfPack; Feb 15th, 2008 at 9:26 pm. Reason: Changed [icode][/icode] tags to [code][/code] tags.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Problem About Trying to Return to Main.

 
1
  #2
Feb 15th, 2008
You should have one overall do...while loop with all your other submenus nested inside with their own do..while loops. This way, when the menu selection has finished its processing, it will loop back to its parent menu (or in the case of the main menu, exit).
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 4
Reputation: Theanthropical is an unknown quantity at this point 
Solved Threads: 0
Theanthropical Theanthropical is offline Offline
Newbie Poster

Re: Problem About Trying to Return to Main.

 
0
  #3
Feb 15th, 2008
so you're saying we should break the codes down to ndividual building blocks?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 4,845
Reputation: joshSCH is on a distinguished road 
Solved Threads: 10
joshSCH's Avatar
joshSCH joshSCH is offline Offline
Banned

Re: Problem About Trying to Return to Main.

 
0
  #4
Feb 15th, 2008
Ehh.. It would perhaps be better to break-up the menu into separate functions.. A new function for each sub-menu.. which could then re-call the main menu at it's conclusion. That's just me, though.. As a java programmer, I love using my own methods
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 4
Reputation: Theanthropical is an unknown quantity at this point 
Solved Threads: 0
Theanthropical Theanthropical is offline Offline
Newbie Poster

Re: Problem About Trying to Return to Main.

 
0
  #5
Feb 15th, 2008
individual methods come through time =]
but we had just learned this for like..2 months. n the handouts doesn't seem to cover some necessary information (like this return thingy -_-') we ended up salvaging bits n pieces here n there to create this.
Last edited by Theanthropical; Feb 15th, 2008 at 10:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Problem About Trying to Return to Main.

 
2
  #6
Feb 15th, 2008
>so you're saying we should break the codes down to ndividual building blocks?
No, I'm not saying that, but it might be a good idea (and would help you see your program's design better).

Just for simplicity's sake, let's say you did break it down into functions. Here's how the overall main menu structure might look:

  1. do
  2. {
  3. printf("Choice: ");
  4. scanf("%d", &choice);
  5.  
  6. switch (choice)
  7. {
  8. case 1:
  9. menu1();
  10. break;
  11. case 2:
  12. menu2();
  13. break;
  14. case 3:
  15. printf("End\n");
  16. break;
  17. }
  18. } while (choice != 3);

Just to reinforce, here's what menu1() might look like:

  1. void menu1()
  2. {
  3. do
  4. {
  5. printf("\n\t\tCHOOSE WHICH SECTION\n");
  6. printf("\t1. Info\n");
  7. printf("\t2. Calculation\n");
  8. printf("\t3. End\n");
  9. printf("Choice: ");
  10. scanf("%s", &pilih);
  11.  
  12.  
  13. switch(pilih){
  14.  
  15. case '1':
  16. printf("Force = Mass * Acceleration\n");
  17. printf("The unit is Newton\n");
  18. break;
  19.  
  20. case '2':
  21. printf("Force = Mass * Acceleration\n\n");
  22. printf("Input Mass in kilogram: ");
  23. scanf("%d", &mass);
  24. printf("Input Acceleration in meter per second second: ");
  25. scanf("%d", &accel);
  26. force = mass * accel;
  27. printf("Force is %d\n", force);
  28. break;
  29.  
  30. case '3':
  31. printf("End\n");
  32. break;
  33.  
  34. default:
  35. printf("Invalid section\n");
  36. break;
  37. }
  38. } while(pilih !=1);
  39. }

Notice I haven't bothered to deal with variables, but hopefully you can see what I'm getting at.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 4
Reputation: Theanthropical is an unknown quantity at this point 
Solved Threads: 0
Theanthropical Theanthropical is offline Offline
Newbie Poster

Re: Problem About Trying to Return to Main.

 
0
  #7
Feb 17th, 2008
but that is just our program simplified by just one function..
n I'm not getting what you're trying to say..
Last edited by Theanthropical; Feb 17th, 2008 at 12:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Problem About Trying to Return to Main.

 
1
  #8
Feb 17th, 2008
You'd have two extra functions: menu1() and menu2() . Try it and see what happens.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC