Output alignment

I pretty much figured out the code that I need to produce the output that is desired. What I don't know and need help with is to get the output to line up because I don't know how. What can I do to get it to line up?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#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;
}

Do you mean like this ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#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 width[] = {7, 8, 5, 5, 3, 4, 4, 6, 9, 7, 8, 8};  // i add this to know the length of each month (how many characters)
    
    
    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++)
     {    
		  int temp=0;
     
          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;
          }
           */
          if (i>0){
				temp = width[i] - width[0];  // i force them to be like the first width, so i use this
		  }		
          cout << months[i] << setw(13-temp) << avgRain[i] << setw(22) << actRain[i] << setw(18) << total << setw(18) << total2 << endl;
     }
     return 0;
}

is this helpful ?
Thank you. It is much appriciated.
Your Welcome :D !
Topic archived. No new replies allowed.