-
Right Justifying output
I need help with right justifying some text. What I want to do is output something like this:
Code:
One Two Three
1 $ 4.99 $ 3.98
2 $ 34.56 $ 905.30
What problem I'm having is that the code is not right justifying like how I want it to in this program. The numbers are variable as they are calculated based on input from the user.
I tried flagging cout as such:
Code:
cout.setf(ios::adjustfield | ios::right);
and
Code:
cout.setf(ios::right);
to try and get this output. Either way didn't work. I believe it works without the $ sign, but not with. Is there a way to get the desired output?
Thanks.
Edit: It does work without the $ sign, but the needs to be there as it is part of the output that the assignment calls for. Thanks.
-
Does this help:
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
post again if you don't get it going
-
It's still not outputting right.
I have these lines:
Code:
cout.setf(ios_base::right, ios_base::basefield);
cout.setf(ios_base::adjustfield);
and these lines:
Code:
cout << "Company" << " " << "Beginning Balance"
<< " " << "Service Fees" << " " << "New Balance";
cout << endl;
cout << setw(4) << "1" << setw(12) << "$ " << balanceCo1
<< setw(14) << "$ " << serviceFeesCo1 << setw(12) << "$ " << newBalanceCo1;
cout << endl;
cout << setw(4) << "2" << setw(12) << "$ " << balanceCo2
<< setw(14) << "$ " << serviceFeesCo2 << setw(12) << "$ " << newBalanceCo2;
cout << endl;
or
Code:
cout << setw(4) << 1 << setw(12) << "$ ";
cout << balanceCo1;
cout << setw(14) << "$ ";
cout << serviceFeesCo1;
cout << setw(12) << "$ ";
cout << newBalanceCo1;
cout << endl;
cout << setw(4) << 2 << setw(12) << "$ ";
cout << balanceCo2;
cout << setw(14) << "$ ";
cout << serviceFeesCo2;
cout << setw(12) << "$ ";
cout << newBalanceCo2;
cout << endl;
doesn't give me the desired output. Thanks.
-
What's it coming out like that's wrong? An example of the output would be nice.
Try shorting your code down to the section that's giving you grief, and post a compilable example here. As it stands, I don't know what variable types we're dealing with etc.
-
I'll probably just forget the $ signs.
Thanks, I'll figure it out sometime else. I don't really have the time to fix the program. You could delete this thread now if you'd like.
-
Well, even if you can't be bothered, I can. Is this what you want:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main(void)
{
float balanceCo1 = 1.1,
serviceFeesCo1 = 12.2,
newBalanceCo1 = 113.3,
balanceCo2 = 224.4,
serviceFeesCo2 = 335.5,
newBalanceCo2 = 446.6;
cout.precision (2);
cout.setf(ios_base::right, ios_base::basefield);
cout.setf(ios_base::fixed, ios_base::floatfield);
cout << setw(4) << 1 << setw(12) << "$ ";
cout << setw(8) << balanceCo1;
cout << setw(14)<< "$ ";
cout << setw(8) << serviceFeesCo1;
cout << setw(12)<< "$ ";
cout << setw(8) << newBalanceCo1;
cout << endl;
cout << setw(4) << 2 << setw(12) << "$ ";
cout << setw(8) << balanceCo2;
cout << setw(14) << "$ ";
cout << setw(8) << serviceFeesCo2;
cout << setw(12) << "$ ";
cout << setw(8) << newBalanceCo2;
cout << endl;
}
/*
Output
1 $ 1.10 $ 12.20 $ 113.30
2 $ 224.40 $ 335.50 $ 446.60
*/