I just need a little advice. This code I have written works well. The only problem I am having is that the output is not lining up and I don't know what to do. If everything was entered line by line I could get it to line up. Since it is done through a for loop I don't know what to do to get them to line up in the output. Any help would be appreciated.

#include<iostream>
#include<iomanip>
#include<cmath>

using namespace std;

int main()
{
    int avgRain[] = {1,2,3,4,5,6,7,8,9,10,11,12};
    int actRain[] = {12,11,10,9,8,7,6,5,4,3,2,1};
    string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    
    int x;
    int total, total2;
    
    cout << "Month" << setw(22) << " Average Rainfall" << setw(22) << " Actual Rainfall" << setw(22) << " Total Below Average" << setw(22) << " Total Above Average" << endl << endl;
    
     for (int i = 0; i < 12; i++)
     {    
          total = avgRain[i] - actRain[i];
          total2 = actRain[i] - avgRain[i];
          //cout << total;
          if( total < 0 )
          {
              total = 0;
              //cout << total;
          }
          if ( total2 < 0)
          {
               total2 = 0;
               //cout << total2;
          }
                   
          cout << months[i] << setw(13) << avgRain[i] << setw(22) << actRain[i] << setw(18) << total << setw(18) << total2 << endl;
    
    
     }
     return 0;
}

Recommended Answers

All 5 Replies

add << right to the cout statement. I like to split up long lines to make them easier to read without horizontally scrolling the code window. Note that I changed some of the widths you had set, but you can use whatever you want.

If you can you might want to display the column titles using two rows of text instead of one. That will make the columns narrower to fit better on the screen.

cout << setw(11) << left << months[i] << setw(8) << right << avgRain[i] << setw(10) 
              << actRain[i] << setw(8) << total << setw(8) << total2 << endl;
commented: Absolutely awesome. +0

Thanks for the help. I don't know why I couldn't think of that.

Experience dear, experience is what helps you do it the right way. Keep up! ;)

Ancient Dragon, you always amaze me with your code.

How do I hash this out on paper so I'm not stuck wondering how to format my output? How should I conceptualize the left/right/internal modifiers being used with setw?

http://www.cplusplus.com/reference/iostream/manipulators/left/

From the code given, is it safe to assume that fill characters are appended to "the end" of the strings and then the beginning of the fill characters to the first set of numbers with a fill-capacity of 19? Or do the right fill characters start from the numbers and overlap/connect with the left fill characters?

Sorry if that question was long-winded. I just want to see if my understanding is correct here. So far I think it's something like the following?

In this example, 'W stands for fill character

End setw(8) from right
                                  V
                         End setw(11) from left
                               V
 J  a  n  u  a  r  y 'W 'W 'W 'W 'W 'W 'W 'W 'W 'W 'W  1
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
 F  e  b  r  u  a  r  y 'W 'W 'W 'W 'W 'W 'W 'W 'W 'W  2
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]

The best way to get this straight in your head is to write a simple program that demonstrates what you do or don't understand. For example if you want to see what happens with a fill character then write a program that does nothing more than cout << setfill('0') << setw(15) << right << fixed << 123 << '\n'; Put just that one line in a program (inside main() of course) and run it to see what happens. Now change something, such as the fill character, compile and run again. Keep changing thigs and experiment to see how your changes affects the output of the program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.