Hey there
I'm new to C programming, fluent in Java. I have a program that I need to create for part of an assignment. This is what I have programmed but for some reason, the program does not allow me to make a second selection, and terminates. Any advice would be appreciated.
Code:
#include <stdio.h>
#include <math.h>
#define pi 3.14159
int main()
{
double al=2.70; /*2.70g/cm3*/
double st=7.85;
double cu=8.94;
char opt1, opt2, metal;
double dens;
printf("Please enter the menu option corresponding \nto the shape of the machined part you wish to build.\n");
printf("A - Cylinder \nB - Rectangle \nC - Cone\nEnter option: ");
scanf("%c", &opt1);
printf("Please enter the menu option corresponding \nto the metal you wish to build the part from: ");
printf("\n1 - Aluminium \n2 - Copper \n3 - Steel\nEnter option: ");
scanf("%c",&opt2);
int check=0;
switch(opt2)
{
case '1':
{
dens=al;
metal=itoa("Aluminium");
check=1;
break;
}
case '2':
{
dens=cu;
metal=itoa("Copper");
check=1;
break;
}
case '3':
{
dens=st;
metal=itoa("Steel");
check=1;
break;
}
default:
{
printf("Invalid option selected.");
break;
}
}
if(check==1)
{
switch(opt1)
{
case 'A':
{
double r,h, vol, weight;
printf("Please enter the radius of the cylinder in cm: ");
scanf("%lf",&r);
printf("Please enter the height of the cylinder in cm: ");
scanf("%lf", &h);
vol=pi*pow(r,2)*h;
weight=vol*dens;
printf("********************\n MACHINE PART \n Shape: Cylinder \n Metal: %c\n Metal density: %lf\n Volume: %lf\n Weight: %lf\n********************\n", metal, dens, vol, weight);
break;
}
case 'B':
{
double l,b,h, vol, weight;
printf("Please enter the length of the rectangle in cm: ");
scanf("%lf",&l);
printf("Please enter the breadth of the rectangle in cm: ");
scanf("%lf",&b);
printf("Please enter the height of the rectangle in cm: ");
scanf("%lf", &h);
vol=l*b*h;
weight=vol*dens;
printf("********************\n MACHINE PART \n Shape: Rectangle \n Metal: %c\n Metal density: %lf\n Volume: %lf\n Weight: %lf\n********************\n", metal, dens, vol, weight);
break;
}
case 'C':
{
double r,h, vol, weight;
printf("Please enter the radius of the cone in cm: ");
scanf("%lf",&r);
printf("Please enter the height of the cone in cm: ");
scanf("%lf", &h);
vol=(1.0/3.0)*pi*pow(r,2)*h;
weight=vol*dens;
printf("********************\n MACHINE PART \n Shape: Cone \n Metal: %c\n Metal density: %lf\n Volume: %lf\n Weight: %lf\n********************\n", metal, dens, vol, weight);
break;
}
default:
{
printf("Invalid option selected.");
break;
}
}
}
}