Click here to Skip to main content
16,020,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a first year comp. sci. major with a [word removed] professor who expects us to know stuff by osmosis. We have no textbook but 500 pages of his notes.

I have written a module that computes FICA taxes and can't seem to get the output correct.

How does one format a cout statement such that this is the output:

1. Jones 235460 $123,000.00 $4,500.00 $545.00
2. Smith 3241 $ 65,000.00 $ 567.00 $ 99.98

and on down for 4 lines.
I have seen examples but what are the underlying rules?

Help please.
Thanks in advance, jim :((
Posted
Updated 17-Feb-11 6:46am
v2
Comments
Sandeep Mewara 17-Feb-11 12:43pm    
Maintain some decorum please!
Emilio Garavaglia 17-Feb-11 15:24pm    
ouch! I don't know what you removed, but check with google the italian-to-english translation for FICA ...
Sandeep Mewara 17-Feb-11 21:38pm    
Just compare the two versions of 'Revision' and you will see.
fjdiewornncalwe 17-Feb-11 19:21pm    
Just so you know, brainflex, you can think what you want about your professor, but get used to it. Once you get into the workforce you will be dealing with people like this all the time, and they often times are paying your salary.

The format controls do not insert either the thousands separator or the currency symbol. To do this you need to use locales and the money_put facet. However this is not pretty.

The following code outputs a value in US dollars:

MSIL
int main()
{
  // Define a locale
  std::locale loc("US");

  // Define the facet
  const std::money_put<char>& output =
    std::use_facet<std::money_put<char> >(loc);

  // Define an iterator to std::cout
  std::ostreambuf_iterator<char, std::char_traits<char> > iterator(std::cout);

  // Use the locale for the output stream
  std::cout.imbue(loc);

  // Ensure that the currency symbol is output
  std::cout.setf(std::ios_base::showbase);

  std::cout << "Value is: ";

  // Output a value to std::cout
  output.put(iterator, false, std::cout, ' ', 12300000.0);
  std::cout << std::endl;
  return 0;
}


If the second parameter to money_put.put is false then the currency symbol is output (e.g. $123,000.00), if set to true then the international monetary sign is used (e.g. USD123,000,00).

The value is interpreted as a value in cents (in general it is the smallest unit of the locale's currency).

However mixing normal values and currency values in the output stream is awkward, imbuing std::cout with a locale affects all subsequent output (which may or may not be what you want). The way round this is to using money_put on a string, and wrap this in a class to make everything easier:

#include <iostream>
#include <iterator>
#include <locale>
#include <string>
#include <sstream>

class MoneyOutputter
{
public:
  MoneyOutputter(const char* const locale_name = "US") : 
      loc(locale_name), 
      output(std::use_facet<std::money_put<char> >(loc)),
      iterator(os)
  {
    os.imbue(loc);
    os.setf(std::ios_base::showbase);
  }

  std::string as_string(double value)
  {
    os.str("");  // clear string
    output.put(iterator, false, os, ' ', value * 100.0);
    return os.str();
  }

private:
  std::locale loc;
  const std::money_put<char>& output;
  std::ostringstream os;
  std::ostreambuf_iterator<char, std::char_traits<char> > iterator;
};

int main()
{
  MoneyOutputter outputter;

  double value1 = 235460.0;
  double value2 = 123000.0;
  double value3 = 4500.0;
  double value4 = 545.0;

  std::cout << "Jones " << value1 << " " << outputter.as_string(value2) << " "
    << outputter.as_string(value3) << " " << outputter.as_string(value4) 
    << std::endl;
  return 0;
}
 
Share this answer
 
v3
Comments
Niklas L 18-Feb-11 4:59am    
Great effort. 5!

An even more appealing way would be to create a Currency class and overload the stream operators, applying your formatting code. It will allow easier use when streaming. It can also be hazardous to store a value with unit in a plain double for a number of reasons.
Take a look at the Format Controls[^] documentation on MSDN. Alternatively you could use one of the printf()[^] functions which also offer similar features.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Feb-11 15:31pm    
It's a 5; what else could be possibly needed?
--SA
Niklas L 17-Feb-11 16:47pm    
Can you really trust a page that says: "The endl manipulator replaces the newline character ('\n')." :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900