Having trouble finding errors
Feb 7, 2014 at 10:07pm UTC
I'm supposed to create a program that calculates maximum deflection by first getting the user input of height, length, base, and weight. Once the maximum deflection is calculated it needs to be displayed in a table format.
The problem is I do not have much experience with C++, I'm having a hard time figuring out where I've made a mistake.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
const double ALUMINUM_ELASTICITY = 1.49e9;
const double WOOD_ELASTICITY = .187e9;
const double STEEL_ELASTICITY = 3.9e9;
double length;
double height;
double base;
double weight;
double max_deflection_aluminum;
double max_deflection_wood;
double max_deflection_steel;
cout << "Enter length in feet: " ;
cin >> length;
cout << endl << "Enter height in feet: " ;
cin >> height;
cout << endl << "Enter base in feet: " ;
cin >> base;
cout << endl << "Enter weight in pounds: " ;
cin >> weight;
cout << endl;
max_deflection_aluminum = (pow( 4 * weight * length, 3)) / (pow( ALUMINUM_ELASTICITY * base * height, 3));
max_deflection_wood = (pow( 4 * weight * length, 3)) / (pow( WOOD_ELASTICITY * base * height, 3));
max_deflection_steel = (pow( 4 * weight * length, 3)) / (pow( STEEL_ELASTICITY * base * height, 3));
cout << fixed << setprecision(2);
cout << setw(10) << "Material" << setw(20) << "Elasticity" << setw(10) << "Length" << setw(10) << "Height" ;
cout << setw(10) << "Base" << setw(10) << "Weight" << setw(20) << "Max Deflection(in)" << endl;
cout << setw(10) << "Aluminum" << setw(20) << ALUMINUM_ELASTICITY << setw(10) << length << setw(10) << height;
cout << setw(10) << base << setw(10) << weight << setw(20) << max_deflection_aluminum << endl;
cout << setw(10) << "Wood" << setw(20) << WOOD_ELASTICITY << setw(10) << length << setw(10) << height;
cout << setw(10) << base << setw(10) << weight << setw(20) << max_deflection_wood << endl;
cout << setw(10) << "Steel" << setw(20) << STEEL_ELASTICITY << setw(10) << length << setw(10) << height;
cout << setw(10) << base << setw(10) << weight << setw(20) << max_deflection_steel << endl;
return 0;
}
Edit: Thanks for the help guys!
Last edited on Feb 7, 2014 at 10:45pm UTC
Feb 7, 2014 at 10:11pm UTC
1 2 3 4
const double length;
const double height;
const double base;
const double weight;
These shouldn't be constant values since you are modifying them.
*edit
On a side note you should spread your output statements across multiple lines to make the code easier to read.
1 2 3 4
cout << setw(10) << "Material" << setw(12) << "Elasticity" << setw(10) << "Length" << setw(10) << "Height" << setw(10) << "Base" << setw(10) << "Weight" << setw(20) << "Max Deflection(in)" << endl;
cout << setw(10) << "Aluminum" << setw(12) << ALUMINUM_ELASTICITY << setw(10) << length << setw(10) << height << setw(10) << base << setw(10) << weight << setw(20) << max_deflection_aluminum << endl;
cout << setw(10) << "Wood" << setw(12) << WOOD_ELASTICITY << setw(10) << length << setw(10) << height << setw(10) << base << setw(10) << weight << setw(20) << max_deflection_wood << endl;
cout << setw(10) << "Steel" << setw(12) << STEEL_ELASTICITY << setw(10) << length << setw(10) << height << setw(10) << base << setw(10) << weight << setw(20) << max_deflection_steel << endl;
I wouldn't suggest past column 80. Some people even go as little as 50.
Last edited on Feb 7, 2014 at 10:12pm UTC
Feb 7, 2014 at 10:24pm UTC
The lines like this make no sense:
cin >> length << endl;
Either add an extra statement to write to
cout
or or append it to the next line like this:
1 2 3 4 5
...
cin >> length;
cout << endl << "Enter height in feet: " ;
...
Also, it's generally not good practice to have
using namespace std
in your code, as it can lead to a lot of naming conflicts. It may be tedious to type std:: in front of everything, but then again there's also find and replace if you're really lazy.
Topic archived. No new replies allowed.