943,695 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 888
  • C RSS
Feb 15th, 2008
0

Problem About Trying to Return to Main.

Expand Post »
hi,I have a problem here.

cpp Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Theanthropical is offline Offline
4 posts
since Feb 2008
Feb 15th, 2008
1

Re: Problem About Trying to Return to Main.

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).
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 15th, 2008
0

Re: Problem About Trying to Return to Main.

so you're saying we should break the codes down to ndividual building blocks?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Theanthropical is offline Offline
4 posts
since Feb 2008
Feb 15th, 2008
0

Re: Problem About Trying to Return to Main.

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
Reputation Points: 1315
Solved Threads: 10
Banned
joshSCH is offline Offline
4,845 posts
since Jul 2005
Feb 15th, 2008
0

Re: Problem About Trying to Return to Main.

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Theanthropical is offline Offline
4 posts
since Feb 2008
Feb 15th, 2008
2

Re: Problem About Trying to Return to Main.

>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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 17th, 2008
0

Re: Problem About Trying to Return to Main.

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Theanthropical is offline Offline
4 posts
since Feb 2008
Feb 17th, 2008
1

Re: Problem About Trying to Return to Main.

You'd have two extra functions: menu1() and menu2() . Try it and see what happens.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Calculating in "C"
Next Thread in C Forum Timeline: Need Help combining programs into one





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC