Solved

C++ using iomanip

Posted on 2014-02-08
3
448 Views
Last Modified: 2014-02-11
I have the following program in C++ and I need to display the cents part of the amount only. I cannot seem to get it to work using iomanip.

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
      
      cout << setiosflags(ios::fixed|ios::showpoint|ios::right);
      
      int quarters, dimes, nickels, pennies;
      float amount;


      cout << "How many quarters? " << endl;
      cin >> quarters;

      cout << "How many dimes? " << endl;
      cin >> dimes;

      cout << "How many nickels? " << endl;
      cin >> nickels;

      cout << "How many pennies? " << endl;
      cin >> pennies;

      amount = (quarters*.25) + (dimes*.10) + (nickels*.05) + (pennies*.01) ;

      cout << "You have " << setprecision(2) << amount << " much money " << endl;

      cout << "You have " << noshowpoint << setprecision(0) << amount << " dollars" << endl;

      cout << "You have " << noshowpoint << setprecision(2) << amount << " cents" << endl;




      return 0;
0
Comment
Question by:CJBOO
3 Comments
 
LVL 30

Assisted Solution

by:Zoppo
Zoppo earned 125 total points
Comment Utility
Hi CJBOO,

I don't think it's possible to format the output using <iomanip> on cout, for floating point values IMO the number before the decimal point can't be completely supressed. So I think the only possibility you have is to calculate the value for cents, i.e. like this:
 cout << "You have " << (int)( 100.0 * ( 0.005 + amount ) ) % 100 << " cents" << endl;

Open in new window

Here I added 0.005 to the calculated value to avoid rounging errors (i.e. without this the resulting int for 1.52 would be 51).

Hope this helps,

ZOPPO
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
std::ios_base::precision -
•Using the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum, and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision.
•In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The digits before the decimal point are not relevant for the precision in this case.
http://www.cplusplus.com/reference/ios/ios_base/precision/

You may wish to review modf:
http://www.cplusplus.com/reference/cmath/modf/
In their example, their output is: 3.141593 = 3.000000 + 0.141593
You know how to handle the whole number. For the decimal part, you would use a method similar to zoppos approach.
0
 
LVL 32

Accepted Solution

by:
sarabande earned 125 total points
Comment Utility
to add to above comments:

it probably is easiest to have two integer variables dollars and cents and do the rounding as Zoppo has shown by cast from double to int.:

int dollars = (int)amount;
int cents = (int)((amount - dollars+0.005)*100);
std::cout << "You have " << dollars << " dollars" << std::endl;
std::cout << "You have " << cents<< " cents" << std::endl;

Open in new window


Sara
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now