Hey i am wondering if it is possible to have a switch statement in a function that is leading to another switch statement and another switch statement afterwards.
Question.
Write a program that will give the user the option to select a shape or a three dimensional object. The program should then prompt the user for the required dimensions and calculate the option the user selects, i.e. perimeter, area, volume, or surface area. Program should be Modular.
When i ran my program , It prompts the user to select shape or 3d objects then afterwards goes on to ask which shape they would like to calculate but unfortunately after i select it, it goes into that function for calculation but it terminates, i have no clue why though.
If anyone can reply to me and tells me it is possible that you can have a case in a case in a case function , i will go fix it again and again till it works.
This is my coding for the question:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int four()
{
float area, perimeter,l,w;
char option;
printf("What Would You like to Calculate: Area Or Perimeter?...Indicate by entering the Initial Letter of the word\n");
scanf("%c", &option);
switch(option)
{
case 'A':
case 'a':
printf("Enter the length and width\n");
scanf("%f", &l);
scanf("%f", &w);
area = l*w;
printf("\nThe area of the shape is %.2f", &area);
getch();
break;
case 'P':
case 'p':
printf("Enter the length and width\n");
scanf("%f", &l);
scanf("%f", &w);
perimeter = (2*l)+(2*w);
printf("\nThe perimeter of the shape is %.2f", &perimeter);
getch();
break;
default:
printf("You have entered the wrong choice...Terminating\n");
break;
}
return 0;
}
int circle()
{
float r,area,circumference;
char option;
printf("\tNote: If you have the diameter of the circle, convert it to its radius by using the formula --> diameter/2\n");
getch();
printf("What do you want to calculate: Circumference or the Area...Indicate by entering the Initial Letter of the word?\n");
scanf("%c", &option);
switch(option)
{
case 'C':
case 'c':
printf("What is the length of the radius?\n");
scanf("%f", &r);
circumference = 2*3.141592654*r;
printf("\nThe circumference of the circle is %.2f", &circumference);
getch();
break;
case 'A':
case 'a':
printf("What is the length of the radius?\n");
scanf("%f", &r);
area = 3.141592654*r*r;
printf("\nThe area of the circle is %.2f", &area);
getch();
break;
default:
printf("That choice does not exist...\n");
break;
}
return 0;
}
int triangle()
{
float h,b,perimeter,area,lg_1,lg_2,lg_3;
char option;
printf("What do you want to find, perimeter or area...Indicate by entering the Initial Letter of the word?\n");
scanf("%c", &option);
switch(option)
{
case 'A':
case 'a':
printf("Enter the height and base of the triangle please..\n");
scanf("%f", &h);
scanf("%f", &b);
area = 0.5*b*h;
printf("The area of the triangle is %.2f", &area);
getch();
break;
case 'P':
case 'p':
printf("\nEnter the length of the sides of the triangle");
scanf("%f", &lg_1);
scanf("%f", &lg_2);
scanf("%f", &lg_3);
perimeter = lg_1+lg_2+lg_3;
printf("\nThe perimeter of the triangle is %.2f", &perimeter);
getch();
break;
default:
printf("Option does not exist mate..\n");
break;
}
return 0;
}
char shape()
{
char option;
printf("NOTE: The shapes that can be calculated involves circles,rectangles,triangles and squares..It is yet to be upgraded\n");
getch();
system("cls");
printf("What shape would you like to find?Indicate by entering the Initial Letter of the word\n");
scanf("%c", &option);
switch(option)
{
case 'S':
case 's':
four();
break;
case 'R':
case 'r':
four();
break;
case 'C':
case 'c':
circle();
break;
case 'T':
case 't':
triangle();
break;
}
return 0;
}
int cube()
{
float volume,area,surface_area,h,l,w;
char option;
printf("What would you like to find out about the cube: volume or surface area..Indicate by entering the Initial Letter of the word\n");
scanf("%c",&option);
switch(option)
{
case 'V':
case 'v':
printf("Enter the height, length and width of the cube please\n");
scanf("%f", &h);
scanf("%f", &l);
scanf("%f", &w);
volume = l*w*h;
printf("\nThe volume of the cube is %.2f", &volume);
break;
case 'S':
case 's':
printf("Enter the length and width of the cube\n");
scanf("%f", &l);
scanf("%f", &w);
area = l*w;
surface_area = 6*(l*w);
printf("\nThe surface area of the cube is %.2f", &surface_area);
break;
}
return 0;
}
int cuboid()
{
float l,w,h,volume,surface_area;
char option;
printf("What would you like to find for the cuboid: surface area or volume?Indicate by entering the Initial Letter of the word\n");
scanf("%c", &option);
switch(option)
{
case 'S':
case 's':
printf("Enter the length,width and height\n");
scanf("%f", &h);
scanf("%f", &l);
scanf("%f", &w);
surface_area = 2*((l*w)+(w*h)+(l*h));
printf("\nThe surface area of the cuboid is %.2f", &surface_area);
break;
case 'V':
case 'v':
printf("Enter the length,width and height\n");
scanf("%f", &h);
scanf("%f", &l);
scanf("%f", &w);
volume = l*w*h;
printf("\nThe volume of the cuboid is %.2f", &volume);
break;
default:
printf("Wrong choice chosen..\n");
exit(EXIT_SUCCESS);
break;
}
return 0;
}
int cylinder()
{
float volume,r,surface_area,h;
char option;
printf("\tNote: If you have the diameter of the circle, convert it to its radius by using the formula --> diameter/2\n");
getch();
system("cls");
printf("Do you want to find the volume or surface area of a cylinder?Indicate by entering the Initial Letter of the word\n");
scanf("%c", &option);
switch(option)
{
case 'V':
case 'v':
printf("Enter the radius and height of the cylinder\n");
scanf("%f", &r);
scanf("%f", &h);
volume = 3.141592654*r*r*h;
printf("\nThe volume of the cylinder was found to be %.2f", &volume);
break;
case 'S':
case 's':
printf("Enter the radius and height of the cylinder\n");
scanf("%f", &r);
scanf("%f", &h);
surface_area = (2*3.141592654*r*r)+ (2*3.141592654*r*h);
printf("\nThe surface area of the cylinder is %.2f", &surface_area);
break;
default:
printf("\nWrong choice selected..");
break;
}
return 0;
}
int sphere()
{
float r,volume,surface_area;
char option;
printf("\tNote: If you have the diameter of the sphere, convert it to its radius by using the formula --> diameter/2\n");
getch();
system("cls");
printf("Do you want to find the volume or surface area of the sphere?Indicate by entering the Initial Letter of the word\n");
scanf("%f", &option);
switch(option)
{
case 'V':
case 'v':
printf("Enter the radius of the sphere\n");
scanf("%f", &r);
volume = (4/3)*3.141592654*r*r*r;
printf("\nThe volume of the sphere is %.2f", &volume);
break;
case 'S':
case 's':
printf("Enter the radius of the sphere\n");
scanf("%f", &r);
surface_area = 4*3.141592654*r*r;
printf("\nThe surface area of the sphere is %.2f");
break;
}
return 0;
}
int tprism()
{
float s,l,h,b,volume,surface_area;
char option;
printf("What do you want to find: volume or surface area of the triangular prism?Indicate by entering the Initial Letter of the word\n");
scanf("%c", &option);
switch(option)
{
case 'V':
case 'v':
printf("Enter the length of the prism, height and base\n");
scanf("%f", &l);
scanf("%f", &h);
scanf("%f", &b);
volume = 0.5*(b*h)*l;
printf("\nThe volume of the prism is %.2f", &volume);
break;
case 'S':
case 's':
printf("Enter the slope's length, length of the prism, height and base\n");
scanf("%f", &s);
scanf("%f", &l);
scanf("%f", &h);
scanf("%f", &b);
surface_area = (b*h)+(2*l*s)+(l*b);
printf("\nThe surface area of the prism is %.2f", &surface_area);
break;
default:
printf("Wrong option selected...\n");
exit(EXIT_SUCCESS);
break;
}
return 0;
}
int main()
{
char dimensional()
{
char choice;
printf("WARNING:The 3D objects that can be calculated includes cube, cuboid, sphere, cylinder, triangular prism.....It is yet to be upgraded\n");
printf("Enter y for cylinder, c for cube, s for sphere, b for cuboid, p for tprism\n\n");
printf("What 3D object do you want to find: cube, cuboid, sphere, cylinder or triangular prism?\n");
scanf("%c", &choice);
switch(choice)
{
case 'Y':
case 'y':
cylinder();
break;
case 'C':
case 'c':
cube();
break;
case 'P':
case 'p':
tprism();
break;
case 'B':
case 'b':
cuboid();
break;
case 'S':
case 's':
sphere();
break;
default:
printf("Wrong choice selected......Terminating\n");
exit(EXIT_SUCCESS);
break;
}
return 0;
}
char option,choice;
printf("Would you like to find calculations for a shape or 3D objects?\n");
printf("Input s for shape and d for 3D objects:");
scanf("%c", &option);
switch(option)
{
case 'S':
case 's':
shape();
break;
case 'D':
case 'd':
dimensional();
break;
default:
printf("Wrong option selected..\n");
exit(EXIT_SUCCESS);
break;
}
return 0;
}