C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 3rd, 2004, 10:56 PM
ddeile ddeile is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 9 ddeile User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old June 4th, 2004, 03:32 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 7
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;
}

Reply With Quote
  #3  
Old June 4th, 2004, 01:34 PM
drizzle drizzle is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: OREGON
Posts: 24 drizzle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
you can also use

cout.setf(ios::showpoint);
cout.precision(2);

to set the precision of the response to two points after the decimal

Reply With Quote
  #4  
Old June 5th, 2004, 01:44 AM
ddeile ddeile is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 9 ddeile User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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;

}

Reply With Quote
  #5  
Old June 5th, 2004, 02:18 PM
drizzle drizzle is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: OREGON
Posts: 24 drizzle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #6  
Old June 5th, 2004, 04:53 PM
ddeile ddeile is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 9 ddeile User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Drizzle,

That worked perfectly. What do the fixed and floatfield arguments do? I have not come across those yet?

Thanks.

Reply With Quote
  #7  
Old June 5th, 2004, 08:03 PM
drizzle drizzle is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: OREGON
Posts: 24 drizzle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > New to C++ Problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek