Its more than likely that I am overlooking something small, but I am
having to write a program that calculates the cost of wood by the
board foot, my problem is that my calculations inside the program is
off
,---- CODE ]
| #include <iostream>
#include <iomanip>
using namespace std;
int ConvertMesurements ( char&, int&, int&, int&, int& );
int main ()
{
int NumberofWood;
int Width;
int Height;
int Length;
int boardcost;
int totalBoardCost;
char ExitStatment;
do {
cout << "Enter Item: ";
cin >> ExitStatment >> NumberofWood >> Width >> Height
>> Length;
boardcost = ConvertMesurements ( ExitStatment,
NumberofWood, Width, Height, Length );
totalBoardCost += boardcost;
} while ( ( ExitStatment != ‘T’ ) || ( ExitStatment != ‘t’ )
);
cout << “Total Cost: $” << setprecision ( 2 ) <<
totalBoardCost;
return 0;
}
int ConvertMesurements ( char& ExitStatment, int& NumberofWood, int&
Width, int& Height, int& Length )
{
int Boardfoot;
int boardcost;
int WoodPrice;
int newLength = 12 * Length;
cout << "New Length: " << newLength << endl;
Boardfoot = ( Width * Height * newLength ) / 144; //This is
where to problem starts, need to figure out why a 2x4x8 is givning a 5
instead of 5.333 repeating.
cout << "Board Foot: " << Boardfoot << endl;
boardcost = Boardfoot * NumberofWood;
cout << "Board Cost: " << boardcost << endl;
switch ( ExitStatment ) {
case ‘P’:
case ‘p’:
WoodPrice = 0.89;
break;
case ‘F’:
case ‘f’:
WoodPrice = 1.09;
break;
case ‘C’:
case ‘c’:
WoodPrice = 2.26;
break;
case ‘M’:
case ‘m’:
WoodPrice = 4.50;
break;
case ‘O’:
case ‘o’:
WoodPrice = 3.10;
break;
default:
cerr << “Error: " << ExitStatment << " is not a valid
kind of wood!” << endl;
}
boardcost = boardcost * WoodPrice;
cout << "Board Cost: " << boardcost << endl;
switch ( ExitStatment ) {
case ‘P’:
case ‘p’:
cout << NumberofWood << " " << Width << “x” << Height
<< “x” << Length << " Pine, cost: $" << setprecision ( 2 ) <<
boardcost << endl;
break;
case ‘F’:
case ‘f’:
cout << NumberofWood << " " << Width << “x” << Height
<< “x” << Length << " Fir, cost: $" << setprecision ( 2 ) << boardcost
<< endl;
break;
case ‘C’:
case ‘c’:
cout << NumberofWood << " " << Width << “x” << Height
<< “x” << Length << " Cedar, cost: $" << setprecision ( 2 ) <<
boardcost << endl;
break;
case ‘M’:
case ‘m’:
cout << NumberofWood << " " << Width << “x” << Height
<< “x” << Length << " Maple, cost: $" << setprecision ( 2 ) <<
boardcost << endl;
break;
case ‘O’:
case ‘o’:
cout << NumberofWood << " " << Width << “x” << Height
<< “x” << Length << " Pine, cost: $" << setprecision ( 2 ) <<
boardcost << endl;
break;
}
return boardcost;
}
`----
When I run it, this is the output that I get
,---- OUTPUT ]
| Enter Item: p 10 2 4 8
New Length: 96
Board Foot: 5
Board Cost: 50
Board Cost: 0
10 2x4x8 Pine, cost: $0
`----
Doing the math by hand this is what I get
,---- CODE ]
| 12*8
96
(2496)/144
5.333 (repeating)
5.333*10
53.333 (repeating)
53.333*0.89
47.47
`----
according to the example that I was given for 10 2x4x8’s the price
should be $47.47 (what I got when I used a calculator).
“We must plan for freedom, and not only for security, if for no other
reason than only freedom can make security more secure.” Karl Popper