iomanip problem?

I'm newbie here...I want to make my out to be like this

Television.............................RM2398.00
DVD Player............................RM 350.00
Biscuits...................................RM 31.50
*******************************
Total.....................................RM2779.50

using iomanip header but, every time I compile it. it not become how it to be.. :(

here is my source code


#include<iostream>
#include<iomanip>

using namespace std;

int main(void)
{
double tv = 2398, dvd = 250, bis =31.5, total;

total = tv+dvd+bis;


cout<<setw(22)<<setfill(' ')<<left<<"Item"<<""<<setw(7)<<setfill(' ')<<right<<"Price"<<endl;
cout<<setw(22)<<setfill('.')<<left<<"Television"<<"RM"<<setw(7)<<setfill(' ')<<right<<tv<<""<<endl;
cout<<setw(22)<<setfill('.')<<left<<"DVD Player"<<"RM"<<setw(7)<<setfill(' ')<<right<<dvd<<""<<endl;
cout<<setw(22)<<setfill('.')<<left<<"Biscuits"<<"RM"<<setw(7)<<setfill(' ')<<right<<bis<<""<<endl;
cout<<setw(31)<<setfill('*')<<""<<endl;
cout<<setw(22)<<setfill('.')<<left<<"Total"<<"RM"<<setw(7)<<setfill(' ')<<right<<total<<""<<endl;


return 0;
}
Last edited on
I tried compiling your code and it works fine with me.
What is your output?
Item Price
Television............RM 2398
DVD Player............RM 250
Biscuits..............RM 31.5
*******************************
Total.................RM 2679.5

this is my output...but i want it be like this

Item Price
Television.............................RM2398.00
DVD Player............................RM 350.00
Biscuits...................................RM 31.50
*******************************
Total.....................................RM2779.50
anyone want to help me
Can you put the actual/expected results in [output][/output] tags?
Otherwise multiple spaces are lost in your post and we can't know what is the result you want
this my output

Item Price
Television............RM 2398
DVD Player............RM 250
Biscuits..............RM 31.5
*******************************
Total.................RM 2679.5


but i want it be like this.

Item                      Price
Television............RM2398.00
DVD Player............RM 350.00
Biscuits..............RM  31.50
*******************************
Total.................RM2779.50
1
2
3
4
5
6
cout<<setw(22)<<setfill(' ')<<left<<"Item"<<""<<setw(9)<<setfill(' ')<<right<<"Price"<<endl;
cout<<setw(22)<<setfill('.')<<left<<"Television"<<"RM"<<setw(7)<<setfill(' ')<<setprecision(2)<<fixed<<right<<tv<<""<<endl;
cout<<setw(22)<<setfill('.')<<left<<"DVD Player"<<"RM"<<setw(7)<<setfill(' ')<<right<<dvd<<""<<endl;
cout<<setw(22)<<setfill('.')<<left<<"Biscuits"<<"RM"<<setw(7)<<setfill(' ')<<right<<bis<<""<<endl;
cout<<setw(31)<<setfill('*')<<""<<endl;
cout<<setw(22)<<setfill('.')<<left<<"Total"<<"RM"<<setw(7)<<setfill(' ')<<right<<total<<""<<endl;
Produces
Item                      Price
Television............RM2398.00
DVD Player............RM 250.00
Biscuits..............RM  31.50
*******************************
Total.................RM2679.50


setprecision and fixed manipulator change the output for floating point numbers.
Thank..it work :)
emm just want to asking. How about showpoint
You'll notice the difference only if you don't use fixed and setprecision(n) with n>=1
1
2
3
4
cout << showpoint << 123. << endl;
cout << noshowpoint << 123. << endl;
cout << fixed << setprecision(2) << showpoint << 123. << endl;
cout << noshowpoint << 123. << endl;
123.000
123
123.00
123.00


http://www.cplusplus.com/reference/iostream/manipulators/showpoint/
Topic archived. No new replies allowed.