Trouble formatting an output

Below is a part of a program that outputs sum, average, smallest amount and largest amount. I have trouble aligning the output into a column, please advise suggestions and errors


sum=(amount1+amount2+amount3+amount4+amount5);
average = ((amount1+amount2+amount3+amount4+amount5)/5);

cout<< setprecision(2) << fixed;

cout << " The sum is: " << setw(6) << sum << endl;
cout << " The average is: " << setw(6) << average <<endl;
cout << "The largest amount is: " << setw(6) << amount3 << endl;
cout << "The smallest amount is:" << setw(6) << amount5;







getch();
}


Output should look as such :

The sum is: 210.00
The average is: 90.00
The largest amount is: 60.00
The smallest amount is: 0.00

What did I do wrong ?

Last edited on
I'm not sure what you mean, but I think you just want your values to be aligned properly. I would also recommend using variables for your denominators when you get a chance.

1
2
3
4
cout << "The smallest amount is: " << setw(5)  << amount5 << endl;
cout << "The largest amount is: "  << setw(6)  << amount3 << endl;
cout << "The average is: "         << setw(13) << average << endl;
cout << "The sum is: "             << setw(17) << sum     << endl;
Helpful! Gracias!
closed account (j2NvC542)
You can tab your output with "\t".
For example:
1
2
3
4
cout << " The sum is:\t" << setw(6) << sum << endl;
cout << " The average is:\t" << setw(6) << average <<endl;
cout << "The largest amount is:\t" << setw(6) << amount3 << endl;
cout << "The smallest amount is:\t" << setw(6) << amount5;


And please use code tags.
Last edited on
Topic archived. No new replies allowed.