| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
|
|
#1
|
|||
|
|||
|
New to C++ Problem
Hi,
I've just started to learn C++ and I am having problems with what I thought would be a simple program. Here is the source code: #include <iostream> using namespace std; int main() { float quantity; float price; float factor; float bondCost; bondCost = ((quantity*price*factor)/100); cout << "Welcome to the bond calculator." << endl; cout << "This program only calculates the cost of a bond."<< endl; cout << "No attempt is made to calcualte the interest." << endl; cout << "" <<endl; cout << "Bond Information" <<endl; cout << "" <<endl; cout << "Enter the quantity: "; cin >> quantity; cout << "Enter the price of the the bond: "; cin >> price; cout << "" <<endl; cout << "The factor must now be calcualted into the principal." <<endl; cout << "Note that if the bond does not have a factor then enter '1'." << endl; cout << "" <<endl; cout << "Enter factor: "; cin >> factor; cout << "" <<endl; cout << "The cost of this bond is: " << bondCost; return 0; } The program is meant to calculate the cost of a bond using the following equation: quantity * price * factor / 100 an example: Quantity of bonds to be purchased: 1,000,000 Price of bonds: 105.25 Factor: .08736524 Cost of Bonds would be: 91,951.9151 1,000,000 * 105.25 * .08736524 = 91,951.9151 Unfortunately the program calculates the cost of the bond to be 0 I've also modified the code as follows: #include <iostream> #include <string> using namespace std; int main() { float quantity; float price; float factor; cout << "Welcome to the bond calculator." << endl; cout << "This program only calculates the cost of a bond."<< endl; cout << "No attempt is made to calcualte the interest." << endl; cout << "" <<endl; cout << "Bond Information" <<endl; cout << "" <<endl; cout << "Enter the quantity: "; cin >> quantity; cout << "Enter the price of the the bond: "; cin >> price; cout << "" <<endl; cout << "The factor must now be calcualted into the principal." <<endl; cout << "Note that if the bond does not have a factor then enter '1'." << endl; cout << "" <<endl; cout << "Enter factor: "; cin >> factor; cout << "" <<endl; cout << "The cost of this bond is: " << ((quantity*price*factor)/100); return 0; } This sort of works. ussing the same values as the first example I get a cost of 91951.9 (it has rounded the number down - not what I want it to do). But if I were to enter the following factor in the equation : .99999999 I get this: 1.0525e+006 What am I doing incorrectly? Thanks for the help. |
|
#2
|
|||
|
|||
|
Ok, theres 3 problems I can see in your source.
1) In the first case you are calculating bondCost before you assign values to the variables so presumably its initialising the other variables to 0 hence your answer is 0. 2) In both cases you are using floats for your variables and MSVC complained at me when I tried to set a float equal to 0.08736524 saying it was truncating from a double. 3) You need to set the precision for cout to something that will cope with your output. (See the following src). Hope this helps, -KM- #include <iostream> #include <string> using namespace std; int main() { double quantity; double price; double factor; double bond_cost; cout << "Welcome to the bond calculator." << endl; cout << "This program only calculates the cost of a bond."<< endl; cout << "No attempt is made to calcualte the interest." << endl; cout << "" <<endl; cout << "Bond Information" <<endl; cout << "" <<endl; cout << "Enter the quantity: "; cin >> quantity; cout << "Enter the price of the the bond: "; cin >> price; cout << "" <<endl; cout << "The factor must now be calcualted into the principal." <<endl; cout << "Note that if the bond does not have a factor then enter '1'." << endl; cout << "" <<endl; cout << "Enter factor: "; cin >> factor; cout << "" << endl; bond_cost = quantity * price * factor / 100; cout.precision (10); cout << "The cost of this bond is: " << bond_cost << endl; return 0; } |
|
#3
|
|||
|
|||
|
you can also use
cout.setf(ios::showpoint); cout.precision(2); to set the precision of the response to two points after the decimal |
|
#4
|
|||
|
|||
|
Thanks,
KM, your suggestion worked. Drizzle, your suggestion (at least as I have coded it - see below) does not set the prescision to 2 decimal places. The output is still displayed in scientific format. #include <iostream> #include <string> using namespace std; int main() { double quantity; double price; double factor; double bondCost; cout << "Welcome to the bond calculator." << endl; cout << "This program only calculates the principal of a bond."<< endl; cout << "No attempt is made to calcualte the interest." << endl; cout << "" <<endl; cout << "Bond Information" <<endl; cout << "" <<endl; cout << "Enter the quantity: "; cin >> quantity; cout << "Enter the price of the the bond: "; cin >> price; cout << "" <<endl; cout << "The factor must now be calcualted into the principal." <<endl; cout << "Note that if the bond does not have a factor then enter '1'." << endl; cout << "" <<endl; cout << "Enter factor: "; cin >> factor; cout << "" <<endl; bondCost = ((quantity*price*factor)/100); cout.setf(ios::showpoint); cout.precision(2); cout << "The pricipal on this bond is: " << bondCost; return 0; } |
|
#5
|
|||
|
|||
|
sorry, i forgot something... it looks like this
cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); cout.precision(2); go ahead and try it |
|
#6
|
|||
|
|||
|
Drizzle,
That worked perfectly. What do the fixed and floatfield arguments do? I have not come across those yet? Thanks. |
|
#7
|
|||
|
|||
|
i duno... probably set something in the cout function. i learned it from my teacher while doing a program..... any other tricks you need, just ask
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > New to C++ Problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|