hey guys im working on a homework problem..im done with 99% of the problem :|however i cnt figure out how to display my output(which is in dollars) using the comma
my output shows >>160000
how would i make it >>160,000
um should i use setprecision???
please help

8 Years
Discussion Span
Last Post by i_luv_c++

So is it:
1. 160 dollar. (one-hundred-sixty)
2. 160.00 dollar (one-hundred-sixty dollar and 00 cents)
3. 160.000 (one-hundred-sixty-thousand)


Edited by Nick Evan: n/a


convert the integer to std::string (use stringstream for that), insert the commas, then display the string. There is no function that will do it for you.

If you use C#, VB.NET or C++/CLR you're in luck because WriteLine() can do it.

Edited by Ancient Dragon: n/a


sorry for not being clear guys...i want to display 160,000 (one-hundred-sixty-thousand)using a comma(,)...
a.dragon>> i am new to c++ so what you saying is use a head file "string" and convert the int variable to string variable ...along with that how would i use this >>std::string<<is this a function??or statement??

#include <string>
#include <sstring>
using std::string;
using std::stringstream;

int main()
   int n = 1234567890;
   stringstream str;
   // insert int into stringstream
   str << n;
   // convert to std::string
   string s = str.str();
   // now just insert commas in appropriate places

Have you learned anything about converting numbers to strings? Any string manipulation at all? If not, you do not have the knowledge yet. Give it a couple weeks and return to this question. You should have the concepts by then.


thanks again guys..no i havent learned how to convert int to strings ...we just finished the chapter on if statements and teachers output had the comma so i thought maybe there is a function using if statement which allows you to do that..again thanks for the help:)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.