if/else program skips to last else statement

//Inertia of 3 Beams

#include <iomanip>
#include <cmath>
#include <iostream>

using namespace std;

int main()
{
int majorBase, minBase, majorHeight, minHeight, radius;
char beamType;
double pi, Ibeam, RectBeam, CylBeam;

pi = 3.14;

//gotostatement here to enter another beam selection//***
//goto to end program here?

//Introduce problem to user and prompt for beam selection
cout << "Today, you will be calculating for the inertia of a beam";
cout << "\nFor the I-beam enter I, \nfor the rectangular beam enter R, \nfor the cylindrical enter C: ";
cin >> beamType;

//based on input make if statements
if (beamType == 'I')
{
//prompt for necessary data needed for calcualtions
cout << "Please enter value for the major base: ";
cin >> majorBase;
cout << "Please enter major height value: ";
cin >> majorHeight;
cout << "Please enter minor base value: ";
cin >> minBase;
cout << "Enter minor height value: ";
cin >> minHeight;

//calculate for Inertia
Ibeam = majorBase*(majorHeight ^ 3) - minBase*(minHeight ^ 3) / 12;
cout << "The moment of Inertia for the I-Beam is: " << Ibeam << "in^4";
}
else if (beamType == 'R')
{

//prompt for necessary data
cout << "Please enter minor base value: ";
cin >> minBase;
cout << "Enter minor height value: ";
cin >> minHeight;

//calcualte for inertia
RectBeam = minBase*(minHeight ^ 3) / 12;
cout << "The moment of inertia for the Rectangular Beam is: " << RectBeam << "in^4";

}
else return(beamType == 'C');
{
//prompt for necessary data
cout << "Please enter radius value: ";
cin >> radius;

//calcualte for inertia
CylBeam = pi*(radius ^ 4) / 4;
cout << "The moment of inertia for the Cylindrical Beam is: " << CylBeam << "in^4";

//goto to go back to prompt asking what beam
//use goto to end program
}

return 0;

}
else return(beamType == 'C');

Should be :

else (beamType == 'C') // No semi-colon (;)
that was how I originally wrote it, but I kept receiving an error saying that a semicolon was expected there.
Last edited on
else (beamType == 'C')

Should be :
else if (beamType == 'C')
thank you so much!
I wouldn't bank on that formula for an I-beam, though. Box section: maybe (since you are subtracting one rectangle from another). I-beam: I think not.


Are you really getting away with statements like
CylBeam = pi*(radius ^ 4) / 4;
That is nonsense C++ coding.
Last edited on
Topic archived. No new replies allowed.