| | |
Problem About Trying to Return to Main.
![]() |
•
•
Join Date: Feb 2008
Posts: 4
Reputation:
Solved Threads: 0
hi,I have a problem here.
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. =]
cpp Syntax (Toggle Plain Text)
#include "stdafx.h" int main() { int speed, dist, time, force, mass, masss, accel, velo, moment; int choice, choose; char pilih, pick; do{ printf("\t\t\tWelcome to Physics (Force and Motion)\n"); printf("\t\t***************************************************\n"); printf("\t\t\tCHOOSE SECTION\n\n"); printf("\t1. FORCE\n"); printf("\t2. SPEED\n"); printf("\t3. MOMENTUM\n"); printf("\t4. END\n\n"); printf("Choice: "); scanf("%d", &choice); switch(choice){ case 1: do{ printf("\n\t\tCHOOSE WHICH SECTION\n"); printf("\t1. Info\n"); printf("\t2. Calculation\n"); printf("\t3. End\n"); printf("Choice: "); scanf("%s", &pilih); switch(pilih){ case '1': printf("Force = Mass * Acceleration\n"); printf("The unit is Newton\n"); break; case '2': printf("Force = Mass * Acceleration\n\n"); printf("Input Mass in kilogram: "); scanf("%d", &mass); printf("Input Acceleration in meter per second second: "); scanf("%d", &accel); force = mass * accel; printf("Force is %d\n", force); break; case '3': printf("End\n"); break; default: printf("Invalid section\n"); }break; }while(pilih !=1); case 2: do{ printf("\t\t\tCHOOSE WHICH SECTION\n"); printf("1.Info\n"); printf("2.Calculation\n"); printf("3.End\n"); printf("Choice: "); scanf("%d", &choose); switch(choose){ case 1: printf("Speed = Distance Taken / Time\n"); printf("The unit in meter per second\n"); break; case 2: printf("Speed = Distance Taken / Time\n"); printf("Input distance in meter:"); scanf("%d", &dist); printf("Input time in second:"); scanf("%d", &time); speed=dist/time; printf("Speed is %d\n", speed); break; case 3: printf("End\n"); break; default: printf("Invalid section\n"); } }while(choose !=2); case 3: do{ printf("\t\t\tCHOOSE WHICH SECTION\n"); printf("1.Info\n"); printf("2.Calculation\n"); printf("3.End\n"); printf("Choice: "); scanf("%s", &pick); switch(pick){ case '1': printf("Momentum = Mass * Velocity\n"); printf("The unit in kilogram meter per second\n"); break; case '2': printf("Momentum = Mass * Velocity\n"); printf("Input Mass in kilogram:"); scanf("%d", &masss); printf("Input Velocity in meter per second:"); scanf("%d", &velo); moment=masss*velo; printf("Momentum is %d", moment); break; case '3': printf("End\n"); break; default: printf("Invalid section\n"); } }while(pick !=3); case 4: printf("Thank you for using this program\n\n"); printf("This program are made by Jakuzzi group\n\n"); printf("The creator are:\n\n"); printf("\t1.MOHAMAD JEFFRI BIN LEE MOHD. RAMLI\n"); printf("\t2.AMAR BIN MOHD. MARITI\n"); printf("\t3.WAN NORAIMI BINTI ZAINAL ABIDIN\n"); printf("\t4.MUHAMMAD DANIAL BIN ISMAIL\n\n"); printf("\tFrom Mecha 2 Sem 2 2008\n"); printf("\tGERMAN-MALAYSIAN INSTITUTE\n"); break; default: printf("Invalid selection\n"); } }while(choice !=4); return 0; }
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.
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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Feb 2008
Posts: 4
Reputation:
Solved Threads: 0
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.
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.
>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:
Just to reinforce, here's what menu1() might look like:
Notice I haven't bothered to deal with variables, but hopefully you can see what I'm getting at.
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:
C Syntax (Toggle Plain Text)
do { printf("Choice: "); scanf("%d", &choice); switch (choice) { case 1: menu1(); break; case 2: menu2(); break; case 3: printf("End\n"); break; } } while (choice != 3);
Just to reinforce, here's what menu1() might look like:
C Syntax (Toggle Plain Text)
void menu1() { do { printf("\n\t\tCHOOSE WHICH SECTION\n"); printf("\t1. Info\n"); printf("\t2. Calculation\n"); printf("\t3. End\n"); printf("Choice: "); scanf("%s", &pilih); switch(pilih){ case '1': printf("Force = Mass * Acceleration\n"); printf("The unit is Newton\n"); break; case '2': printf("Force = Mass * Acceleration\n\n"); printf("Input Mass in kilogram: "); scanf("%d", &mass); printf("Input Acceleration in meter per second second: "); scanf("%d", &accel); force = mass * accel; printf("Force is %d\n", force); break; case '3': printf("End\n"); break; default: printf("Invalid section\n"); break; } } while(pilih !=1); }
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.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- bank system problem !!!! (C++ programming) (C++)
- Function Back to Main (C++)
- Return variable (C++)
- Ad/Spy/Malware problem that I can't fix (Viruses, Spyware and other Nasties)
- System 32 startup problem (Viruses, Spyware and other Nasties)
- C++ pointers problem (C++)
- Scanf problem (C)
- C pointer problem (C)
- need help problem with cin.getline method (C++)
- Stack Queue Fstream (C++)
Other Threads in the C Forum
- Previous Thread: Calculating in "C"
- Next Thread: Need Help combining programs into one
| Thread Tools | Search this Thread |
* adobe ansi api array arrays bash binarysearch calculate centimeter char cm convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hardware highest homework i/o ide inches initialization intmain() iso km linked linkedlist linux linuxsegmentationfault list logical_drives loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






