Thread: I am completely at a loss

1. I am completely at a loss

So ive been trying to figure out how to make this program continue to allow the user to perform as many calculations as they want. im using nested switch statements, but i cant seem to exit switches... im so lost. ive added so many if/ and exit conditions that at this point im considering starting over. Please help.

Code:
```#include <stdio.h>
#include <stdlib.h>
#define pi 3.141593
//Author:
//Descripiton:This program can calculate the Perimeters/Areas and volumes of shapes.
//Date 4/1/14
//function prototypes
float volSphere(float r);
float volCylinder(float r, float h);
float volCone(float r, float h);
int main()
{
//variable declarations
int n,m,q,z;
float l,w,h,r;
//prompt for first switch statement
printf("This program calculates the Perimeters/Areas/Volumes of various 3-Dimensional and 2-Dimensional shapes. Which operation do you wish to perform? [-1 to end] \n\n");
printf("1)Perimeter\n");
printf("2)Area\n");
printf("3)Volume\n");
scanf("%d",&n);
do
{
switch(n)
{
//
case 1:
printf("For which shape would you like to calculate the Perimeter?[-1 to end]\n\n");
printf("1)Square\n");
printf("2)Rectangle\n");
printf("3)Parallelogram\n");
printf("4)Triangle\n");
printf("5)Trapezoid\n");
printf("6)Circle\n");

scanf("%d",&m);
if(m<0)
{
exit(0);
}else{
do
{
//nested switch statement
switch(m)
{
case 1:
printf("Enter the side length of the square: \n");
scanf("%f",&l);
printf("The perimeter of the square with side length %.2f units is %.2f units.\n\n",l,4*l);
break;
case 2:
printf("Enter the length of the rectangle: \n");
scanf("%f",&l);
printf("Enter the width of the rectangle: \n");
scanf("%f",&w);
printf("The perimeter of the rectangle with side length %.2f units and width %.2f units is %.2f units.\n\n",l,w,2*l+2*w);
break;
case 3:
printf("Enter the length of the base of the parallelogram: \n");
scanf("%f",&l);
printf("Enter the side of the parallelogram: \n");
scanf("%f",&w);
printf("The perimeter of the parallelogram with base %.2f units and side length %.2f units is %.2f units.\n\n",l,w,2*l+2*w);
break;
case 4:
printf("Enter side 1 of the triangle: \n");
scanf("%f",&l);
printf("Enter side 2 of the triangle: \n");
scanf("%f",&w);
printf("Enter side 3 of the triangle: \n");
scanf("%f",&h);
printf("The perimeter of the triangle with sides: %.2f units, %.2f units and %.2f units is %.2f units.\n\n",l,w,h,(l+w+h));
break;
case 5:
printf("Enter side 1 of the trapezoid: \n");
scanf("%f",&l);
printf("Enter side 2 of the trapezoid: \n");
scanf("%f",&w);
printf("Enter side 3 of the trapezoid: \n");
scanf("%f",&h);
printf("Enter side 4 of the trapezoid: \n");
scanf("%f",&r);
printf("The perimeter of the trapezoid with sides: %.2f units, %.2f units, %.2f units and %.2f units is %.2f units\n\n",l,w,h,r,(l+w+h+r));
break;
case 6:
printf("Enter the radius of the circle: \n");
scanf("%f",&r);
printf("The Perimeter/Circumference of the circle with radius %.2f units is %.2f units\n\n",r,2*pi*r);
break;
}
break;
}while(m>0);
}
break;//need to break
case 2:
printf("For which shape would you like to calculate the Area?[-1 to end]\n\n");
printf("1)Square\n");
printf("2)Rectangle\n");
printf("3)Parallelogram\n");
printf("4)Triangle\n");
printf("5)Trapezoid\n");
printf("6)Circle\n");
printf("7)Cylinder\n");
scanf("%d",&q);
if(q<0)
{
exit(0);
}else{
do
{
//2nd nested switch statement
switch(q)
{
case 1:
printf("Enter the side length of the square: \n");
scanf("%f",&l);
printf("The area of the square with side length %.2f units, is %.2f units.\n",l,l*l);
break;
case 2:
printf("Enter the side length of the rectangle: \n");
scanf("%f",&l);
printf("Enter the side width of the rectangle: \n");
scanf("%f",&w);
printf("The area of the rectangle with length %.2f units and %.2f units is %.2f units.\n",l,w,l*w);
break;
case 3:
printf("Enter the base length of the parallelogram: \n");
scanf("%f",&l);
printf("Enter the height of the parallelogram: \n");
scanf("%f",&w);
printf("The area of the parallelogram with base %.2f units and height %.2f units is %.2f units.\n",l,w,l*w);
break;
case 4:
printf("Enter the base length of the triangle: \n");
scanf("%f",&l);
printf("Enter the height of the triangle: \n");
scanf("%f",&w);
printf("The area of the triangle with base %.2f units and height %.2f units is %.2f units.\n",l,w,(l*w)/2.0);
case 5:
printf("Enter the lower base of the trapezoid: \n");
scanf("%f",&l);
printf("Enter the upper base of the trapezoid: \n");
scanf("%f",&w);
printf("Enter the height of the trapezoid: \n");
scanf("%f",&h);
printf("The area of the trapezoid with bases %.2f units and %.2f units and height %.2f units is %.2f units.\n",l,w,h,(l+w)*h/2);
break;
case 6:
printf("Enter the radius of the circle: \n");
scanf("%f",&r);
printf("The area of the circle with radius %.2f units is %.2f units.\n",r,pi*r*r);
break;
case 7:
printf("Enter the radius of the cylinder: \n");
scanf("%f",&r);
printf("Enter the height of the cylinder: \n");
scanf("%f",&h);
printf("The surface area of the cylinder with radius %.2f units and height %.2f units is %.2f units.\n",r,h,(2*pi*r*h)+(2*pi*r*r));
break;
}
break;
}while(q>0);
}
break; //need to break
case 3:
printf("For which shape would you like to calculate the Volume?[-1 to end]\n\n");
printf("1)Cube\n");
printf("2)Rectangular Prism\n");
printf("3)Triangular Pyramid\n");
printf("4)Sphere\n");
printf("5)Cylinder\n");
printf("6)Cone\n");
do
{
scanf ("%d",&z);
//if(z<0)
//{
//    exit(0);
//}else{
//do
//{
//4rd nested switch statement
switch(z)
{
case 1:
printf("Enter the side length of the cube: \n");
scanf("%f",&l);
printf("The volume of the cube with side length %.2f units is %.2f units.\n",l,l*l*l);
break;
case 2:
printf("Enter the base length of the rectangular prism: \n");
scanf("%f",&l);
printf("Enter the base width of the rectangular prism: \n");
scanf("%f",&w);
printf("Enter the height of the rectangular prism: \n");
scanf("%f",&h);
printf("The volume of the rectangular prism with base length and width %.2f units and %.2f units \
and height %.2f units is %.2f units.\n",l,w,h,l*w*h);
break;
case 3:
printf("Enter the base length of the triangular pyramid: \n");
scanf("%f",&l);
printf("Enter the base width of the triangular pyramid: \n");
scanf("%f",&w);
printf("Enter the height of the the triangular pyramid: \n");
scanf("%f",&h);
printf("The volume of the triangular pyramid with base length and width %.2f units and %.2f units and height %.2f units is %.2f units.\n",l,w,h,(l*w*h)/3.0);
break;
case 4:
printf("Enter the radius of the sphere: \n");
scanf("%f",&r);
printf("The volume of the sphere with radius %.2f units is %.2f units.\n",r,volSphere(r));
case 5:
printf("Enter the radius of the cylinder: \n");
scanf("%f",&r);
printf("Enter the height of the cylinder: \n");
scanf("%f",&h);
printf("The volume of the cylinder with radius %.2f units and height %.2f units is %.2f units.\n",r,h,volCylinder(r,h));
break;
case 6:
printf("Enter the radius of the cone: \n");
scanf("%f",&r);
printf("Enter the height of the cone: \n");
scanf("%f",&h);
printf("The volume of the cone with radius %.2f units and height %.2f units is %.2f units.\n",r,h,volCone(r,h));
break;
}
//}
}while(z>0);
break;
}//end of switch to "n" PAV
}while(n>0);
return 0;
}
//function definitions
float volSphere(float r)
{
float vol;
vol=(4*pi*r*r*r)/3.0;
return vol;
}
float volCylinder(float r, float h)
{
float vol;
vol=pi*r*r*h;
return vol;
}
float volCone(float r, float h)
{
float vol;
vol=(pi*r*r*h)/3.0;
return vol;
}```

2. Tip: break down large functions that perform many different tasks into smaller functions that do one thing and do it well. Then, your larger function would just have the task of delegating the smaller units of work to those smaller functions.

3. like what? if you cant tell im completely new to programming :/ any and all help would be appreciated

4. For example, you could move each switch statement into its own function. Or, maybe the block of code covered by a case within the switch can go into a function. You should be able to give a name to the function, e.g., prompt_for_shape, read_shape_input, read_rectangle_input, etc.

5. thanks ill definately try that. Ive made a mess trying to bound my menus to the point that its really hard for me to even read it anymore let alone someone who didnt write it so ill probably just start over with your suggestion.

6. Agreed. Break your switch statements up into functions.

ie return; the variables you require to carry on to your next task.

Maybe nest it all within a single While loop that will always be true unless scanf gets a 'q' for example.

7. I agree with them, you should really use functions for these things, cuz it's too much lines in main. and I'd like to add if you want to make the user perform as many operations as they want, well for start while loop is good for this.
like for example: (let's say the user can't use negative numbers)
Code:
```//...start
scanf("%d", &q);
while(q > 0)
{
//perform things...
scanf("%d", &q);
// considering q is an int
}
//end...```
Or you can also user EOF (end of file) in while too

Popular pages Recent additions