1

I am trying to create an organized looking graph with the amount of money for each food under each month. I was able to line up the first row, but after that, something started going off. This is what I ended up with:Current Output

This is the code I tried playing around with the setw() but I can't seem to change it for the number after it puts out the first array element.

void sales_report() { for (int i = 0; i < 7; i++) { cout << setw(17) << months[i] << " "; } cout << endl << foods[0] << " "; for (int i = 0; i < 6; i++) { sumapple += apples[i]; cout << setprecision(2) << fixed << setw(8) << money << apples[i] << " "; } cout << setprecision(2) << fixed << setw(8) << money << sumapple << endl; cout << foods[1] << " "; for (int i = 0; i < 6; i++) { sumoranges += oranges[i]; cout << setprecision(2) << fixed << setw(7) << money << oranges[i] << " "; } cout << setprecision(2) << fixed << setw(7) << money << sumoranges << endl; cout << foods[2] << " "; for (int i = 0; i < 6; i++) { sumpears += pears[i]; cout << setprecision(2) << fixed << setw(10) << money << pears[i] << " "; } cout << setprecision(2) << fixed << setw(10) << money << sumpears << endl; cout << foods[3] << " "; for (int i = 0; i < 6; i++) { sumtomatoes += tomatoes[i]; cout << setprecision(2) << fixed << setw(7) << money << tomatoes[i] << " "; } cout << setprecision(2) << fixed << setw(7) << money << sumtomatoes << endl; cout << foods[4] << " "; for (int i = 0; i < 6; i++) { sumcherries += cherries[i]; cout << setprecision(2) << fixed << setw(6) << money << cherries[i] << " "; } cout << setprecision(2) << fixed << setw(6) << money << sumcherries << endl << totalmonth; for (int i = 0; i < 6; i++) { total = apples[i] + oranges[i] + pears[i] + tomatoes[i] + cherries[i]; cout << setprecision(2) << fixed << setw(9) << money << total << " "; } } 
0

1 Answer 1

2

You are using different std::setw() values in each of the for loops. Hence the misalignment. Use the same values.

Sign up to request clarification or add additional context in comments.

5 Comments

That worked for the spacing problem, but if I keep them all the same how do I makeup for the fact that the food names are different lengths
Use the std::setw(15) for example when printing out those too. You don't set the width currently.
My teacher won't allow it. He said we are only allowed to align right and if I apply setw to those then it will move the word to the right. I know it would be easy to just use left and it would look fine in output but its not allowed for this.
@t.slez Are you allowed to calculate the length of the string with the food name (please, tell me it's a std::string...) and then adjust the width of the first price accordingly?
Is it possible to adjust the width of the first price within the for loop?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.