hi,I have a problem here.

#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. =]

Recommended Answers

All 7 Replies

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).

so you're saying we should break the codes down to ndividual building blocks?

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 :)

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.

>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:

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:

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.

commented: I agree :) +12

but that is just our program simplified by just one function..
n I'm not getting what you're trying to say..

You'd have two extra functions: menu1() and menu2() . Try it and see what happens.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.