openSUSE Forums > Programming/Scripting » Debugging my math in C++

Go Back   openSUSE Forums > Programming/Scripting
Forums FAQ Members List Search Today's Posts Mark Forums Read


Programming/Scripting Questions about programming, bash scripts, perl, php, cron jobs, ruby, python, etc.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-Nov-2009, 11:06
vendion's Avatar
Busy Penguin
 
Join Date: Jun 2008
Location: Cleveland, TN USA
Posts: 384
vendion hasn't been rated much yet
Send a message via ICQ to vendion Send a message via AIM to vendion Send a message via MSN to vendion Send a message via Yahoo to vendion
Default Debugging my math in C++

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

(2*4*96)/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
Reply With Quote
  #2 (permalink)  
Old 07-Nov-2009, 17:26
Explorer Penguin
 
Join Date: Sep 2008
Posts: 161
dmera hasn't been rated much yet
Default Re: Debugging my math in C++

As you said you are overlooking a small thing. The result of the division you are save it in an integer instead of a float. Just do a print after you input the values and see how they are stored as I can see that all your variables are integers. declare Board and all the ones you want to have them in decimals as float and you'll have the results working properly.
cheers
Reply With Quote
Reply

Bookmarks


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

Search Engine Friendly URLs by vBSEO 3.3.0 RC2