Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 03-14-2007, 09:43 AM   #1
Member (9 bit)
 
Sam-U-Rai's Avatar
 
Join Date: Sep 2006
Location: NYC
Posts: 272
Trying to set Decimals in C++

I want to set the precision of the variable total_price to only 2 decimal places (EX: 2.82)

Here's the code:
#include
#include

//Price It
//3-13-07
double original_price (void);
double discount_price (void);
double calculate_price (double, double);

int main ()

{
double price;
double discount;
double total_price;
double tax;

for(int x=1; x<6; x++)
{
price = original_price();
discount = discount_price();

total_price = calculate_price(price,discount);

cout< cout< cout<
cout< cout<
cout<THIS IS WHERE I WANT THE 2 DECIMAL PLACES
cout<
}
return 0;

}


double original_price (void)
{
double price;

cout<<"What is the original price of the item?"< cin>>price;
cout< return price;
}


double discount_price (void)
{
double discount;

cout<<"What is the size of the discount? (in decimal value)"< cin>>discount;
cout< return discount;

}

double calculate_price (double price, double discount)

{
double total_price;
double tax;
tax = 0.06;
price = price - price * discount;
total_price = price * tax + price;


return total_price;

}
Sam-U-Rai is offline   Reply With Quote
Old 03-14-2007, 10:15 AM   #2
FLG
Member (11 bit)
 
FLG's Avatar
 
Join Date: May 2003
Location: Brooklyn, NY
Posts: 1,798
Send a message via AIM to FLG
Before that line just put.

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

You also need to #include < iomanip > (sorry for the spaces, the forum makes it not be able to be seen without them)

You should just be able to put the "cout.precision (2);" but i've been taught toput the other stuff to. showpoint is used to show something like 22.00, where it will still display the ".00" and fixed just sets it to a fixed point notation.

Last edited by FLG; 03-14-2007 at 10:17 AM.
FLG is offline   Reply With Quote
Old 03-14-2007, 03:30 PM   #3
Member (9 bit)
 
Sam-U-Rai's Avatar
 
Join Date: Sep 2006
Location: NYC
Posts: 272
Quote:
Originally Posted by FLG
Before that line just put.

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

You also need to #include < iomanip > (sorry for the spaces, the forum makes it not be able to be seen without them)

You should just be able to put the "cout.precision (2);" but i've been taught toput the other stuff to. showpoint is used to show something like 22.00, where it will still display the ".00" and fixed just sets it to a fixed point notation.
Thanks for the advice. I know about the Manip. I actually have that included but this forum doesn't let me do stuff with < so alot of the code is missing.

Anyway, thanks. That worked. Thanks so much !
Sam-U-Rai is offline   Reply With Quote
Old 03-14-2007, 04:05 PM   #4
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
If you wrap your C++ code with PHP code tags it will display everything properly.

PHP Code:
#include <likethis.c> 
doctorgonzo is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Spyware help Chad44 Networking & Online Security 2 06-14-2005 06:23 PM
Service Disabling Jates Windows Legacy Support (XP and earlier) 2 04-11-2005 09:27 PM
Trying to set up an Active Directory, win 2000 Nayon Networking & Online Security 1 03-16-2005 08:50 AM
A7N8X BIOS not detecting RAID set foghat46 Computer Hardware 5 08-22-2003 11:16 PM
Server Set up Continued SteveAH10 Internet, Web Applications, & The Cloud 1 08-13-2002 10:46 PM


All times are GMT -5. The time now is 07:26 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2