Hi Everyone. IN NEED OF HELP ASAP ! my deadline is today and im having trouble with this simple programme. i just could not figure out the problem. after the second switch, the programme closes. HELP ME .
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.1416
void main (void)
{
int choice;
char choiceA, choiceB, choiceC;
float Height, Radius, Breadth, Area, SurfaceArea, Volume, BaseLength, Length;
printf("Geometry Calculator\n");
printf("This programme calculates the area, volume and surface area of various shapes.\n\n");
printf("Selection Menu\n\n");
printf("1.Area\n");
printf("2.Surface Area\n");
printf("3.Volume\n");
printf("0.Exit\n");
printf("Enter Choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("A. Circle\n");
printf("B. Triangle\n");
printf("C. Square\n");
printf("D. Rectangle\n");
printf("Enter Your Choice: ");
scanf("%c", &choiceA);
switch (choiceA)
{
case 'A':
printf("Enter the radius of the circle (in cm): ");
scanf("%f", &Radius);
if(Radius > 0)
{
Area = PI * pow(Radius,2);
printf("The area of the circle: %f cm sq", Area);
}
else
{
printf("Invalid Radius. (Make sure the radius is a postive value)");
}
break;
case 'B':
printf("Enter the base length of the triangle (in cm): ");
scanf("f", &BaseLength);
printf("Enter the height of the triangle (in cm): ");
scanf("f", &Height);
if(BaseLength > 0 && Height >0)
{
Area = (1.0/2.0) * BaseLength * Height;
printf("The area of the triangle: %f cm sq", Area);
}
else
{
printf("Invalid Base Length or Height or Both. (Make sure values are positive)");
}
break;
case 'C':
printf("Enter the length of the square: ");
scanf("f", &Length);
if(Length > 0)
{
Area = pow(Length,2);
printf("The area of the square: %f cm sq", Area);
}
else
{
printf("Invalid Length. (Make sure the value is positive)");
}
break;
case 'D':
printf("Enter the base length of the rectangle (in cm): ");
scanf("%f", &BaseLength);
printf("Enter the height of the rectangle (in cm): ");
scanf("%f", &Height);
if(BaseLength > 0 && Height > 0)
{
Area = BaseLength * Height;
printf("The area of the rectangle: %f cm sq", Area);
}
else
{
printf("Invalid Base Length or Height or Both. (Make sure values are positive)");
}
break;
default :
printf("Unknown Choice\n");
}
break;
case 2:
printf("A. Sphere\n");
printf("B. Cylinder\n");
printf("C. Cube\n");
printf("D. Rectangular Prism\n");
printf("Enter Your Choice: ");
scanf("%c", &choiceB);
switch(choiceB)
{
case 'A':
printf("Enter the radius of the sphere (in cm): ");
scanf("%f", &Radius);
if(Radius > 0)
{
SurfaceArea = 4 * PI * pow(Radius,2);
printf("The surface area of the sphere: %f cm sq", SurfaceArea);
}
else
{
printf("Invalid Radius. (Make sure radius is positive)");
}
break;
case 'B':
printf("Enter the radius of the cylinder (in cm): ");
scanf("%f", &Radius);
printf("Enter the height of the cylinder (in cm): ");
scanf("%f", &Height);
if(Radius > 0 && Height > 0)
{
SurfaceArea = ( 2 * PI * pow(Radius,2)) + ( 2 * PI * Radius * Height);
printf("The surface area of the cylinder: %f cm sq", SurfaceArea);
}
else
{
printf("Invalid Radius or Height or Both. (Make sure values are positive)");
}
break;
case 'C':
printf("Enter the base length of the cube (in cm): ");
scanf("%f", &BaseLength);
if(Radius > 0)
{
SurfaceArea = 6 * pow(BaseLength,2);
printf("The surface area of the cube: %f cm sq", SurfaceArea);
}
else
{
printf("Invalid Base Length. (Make sure Base Length is positive)");
}
break;
case 'D':
printf("Enter the length of the rectangular prism (in cm): ");
scanf("%f", &Length);
printf("Enter the breadth of the rectangular prism (in cm): ");
scanf("%f", &Breadth);
printf("Enter the height of the rectangular prism (in cm): ");
scanf("%f", &Height);
if(Length > 0 && Breadth > 0 && Height > 0)
{
SurfaceArea = (( 2 * Length * Breadth ) + ( 2 * Breadth * Height ) + ( 2 * Length * Height ));
printf("The surface area of the rectangular prism: %f cm sq", SurfaceArea);
}
else
{
printf("Invalid Length, Breadth or Height or ALL. (Make sure values are postive)");
}
break;
default:
printf("Unknown Choice\n");
}
break;
case 3:
printf("A. Cube\n");
printf("B. Cylinder\n");
printf("C. Cone\n");
printf("D. Sphere\n");
printf("Enter Your Choice: ");
scanf("%c", &choiceC);
switch(choiceC)
{
case 'A':
printf("Enter the base length of the cube (in cm): ");
scanf("%d", &BaseLength);
if(BaseLength > 0)
{
Volume = pow(BaseLength,3);
printf("The volume of the cube: &f cm cube", Volume);
}
else
{
printf("Invalid Base Length. (Make sure base length is positive)");
}
break;
case 'B':
printf("Enter the radius of the cylinder (in cm): ");
scanf("%f", &Radius);
printf("Enter the height of the cylinder (in cm): ");
scanf("%f", &Height);
if(Radius > 0 && Height > 0)
{
Volume = PI * pow(Radius,2) * Height;
printf("The volume of the cylinder: %d cm cube");
}
else
{
printf("Invalid Radius or Height or Both. (Make sure values are positive)");
}
break;
case 'C':
printf("Enter the radius of the cone (in cm): ");
scanf("%f", &Radius);
printf("Enter the height of the cone (in cm): ");
scanf("%f", &Height);
if(Radius > 0 && Height > 0)
{
Volume = (1.0/3.0) * pow(Radius,2) * PI * Height;
printf("The volume of the cone: %f cm cube", Volume);
}
else
{
printf("Invalid Height or Radius or Both. (Make sure values are positive)");
}
break;
case 'D':
printf("Enter the radius of the sphere (in cm): ");
scanf("%f", &Radius);
if(Radius > 0)
{
Volume = (4.0/3.0) * pow(Radius,3);
printf("The volume of the sphere: %f cm cube", Volume);
}
else
{
printf("Invalid Radius. (Make sure radius is positive)");
}
break;
default:
printf("Unknown Choice\n");
}
break;
case 0:
printf("Goodbye! Thank you for using this programme!\n");
break;
default:
printf("Unknown Choice\n");
}
getch();
exit(0);
}
Your problem comes from the fact you do not tell you program to loop back at the beginning of the menu if the choice was not no exit.
To correct that you can use a while loop around your menu:
1 2 3 4 5 6 7 8 9 10 11
int main (void)
{
// variables declaration
bool ExitProgram = false;
while( ExitProgram == false )
{
// your current menu code
// for case 0, add "ExitProgram = true;" before break
}
// ...
}
i have decided to abandon this program and create a new program instead. i will make a simpler program. the requirements were to only include if-else and switch. i may have been too adventurous and in the end myself up in deep trouble. LOL . thanks for replying me anyway. sorry to waste your time.