Simple problem with formating text

When the showarray function is called and the numbers are displayed next to the months, the numbers are not in a column but scattered around. I tried to use setw() but I don't think it works they way I think it works... if that makes sense. Anyway I'm sure that there is a way to format this so that the numbers appear neatly in a column next to the month but I don't know how to make them do so.

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
 // Rainfall FP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


void showarray(string [], double [], int);
double getTotal(double [], int);
double getAverage(double [], int);
void sortRainfall(string [], double [], int);

int main ()
{
	const int size = 12;
	string month [size] = {"January", "February", "March", "April",
						   "May", "June", "July", "August",
						   "September", "October",
						   "November", "December"};
	double rainfall[size]= {0};
	
	cout<<setw(20)<<"Rainfall Data Program"<<endl;
	cout<<setw(20)<<"----------------------"<<endl;
	cout<<endl;
	
	cout<< "Please enter the rainfall amount in inches for each month." <<endl;
	cout<<endl;

	for (int index = 0; index < size; index++)
	{
			cout<< month[index]<< ": "; 
			cin>> rainfall[index]; 
			
			while (rainfall[index] < 0 || rainfall[index] > 100) //Validate data (100 in. is based on the maximum
				                                                 //record of rainfall in 1 month in the U.S)
			{
				cout<< "The number you have enterd is invalid." <<endl;
				cout<< "Please try again" <<endl<<endl;
				
				cout<< month[index]<< ": "; 
				cin>> rainfall[index]; 
			}
	}

	cout<<endl;
	
	showarray(month, rainfall, size);

	cout<< "The total rainfall for the year is: "<<getTotal(rainfall, size )<<"in"<<endl;
	cout<< "The average monthly rainfall is:    "<<getAverage(rainfall, size)<< "in"<<endl;
	cout<<endl;
	cout<< "The monthly rainfall from highest to lowest is: "<<endl;
	cout<< "------------------------------------------------"<<endl;
	cout<<endl;

	sortRainfall(month, rainfall, size);

	showarray(month, rainfall, size);

	cout<<endl;cout<<endl;
	
	
	
}
//**********************FUNCTIONS*************************************************

void showarray(string montharray[], double rainfallarray[], int size)
{
	cout<< "Month"<<setw(20)<< "Rainfall (in)" <<endl;
	cout<< "-----"<<setw(10)<< "----------------" <<endl;
	
	for( int index = 0; index < size; index++)
	{
		cout<< montharray[index] << setw(10)<< rainfallarray[index] <<endl;
	
	}

	cout<<endl;

}
		
double getTotal(double rainfallarray[], int size)
{
	double total = 0;
	
	for (int index = 0; index < size; index++)
		total+= rainfallarray[index];

	return total;
}

double getAverage (double rainfallarray[], int size)
{
	double total = 0;
	double average = 0;

	for (int index = 0; index < size; index++)
		total+= rainfallarray[index];
		average=total / size;

return average;
}

void sortRainfall(string month[], double rainfall[], int size)
{
	int startScan, maxIndex; 
	string tempMo;
	double maxValue; 

	for (startScan = 0; startScan < (size - 1); startScan++)
	{
		maxIndex = startScan;
		maxValue = rainfall[startScan];
		tempMo = month[startScan];
		for(int index = startScan +1; index < size; index++)
		{
			if (rainfall[index] > maxValue)
			{
				maxValue = rainfall[index];
				tempMo = month[index];
				maxIndex = index;
			}
		}
		rainfall[maxIndex] = rainfall[startScan];
		month[maxIndex] = month[startScan];
		rainfall[startScan] = maxValue;
		month[startScan] = tempMo;
	}
}


	
IO manipulators you are trying to use only have to do with representation of decimals. One solution for your problem would be to cout month names after processing them with some sort of length normalizing function, like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// You may use optional argument c, in case you wish to prefill
// the rest of the string with eg. dots. If you'd like to create
// tables of contents for example
string setLength (string input, int n, char c = ' ' ) 
{
	// Instead of creating empty string - 
	// prefill it with c's. The string
	// will contain n times the character c
	string output(n, c);

	// Now copy the required amount of letters from
	// the input. If there are too many, they will
	// be cut off. If there are too little, the rest
	// will be spaces (or whatever c is).
	for (int i = 0; i < n && i < input.size(); i++)
	{
		output[i] = input[i];
	}
	return output;
}


Now, lines around 70 of your code:
cout<< montharray[index] << setw(10)<< rainfallarray[index] <<endl;
would be changed to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for( int index = 0; index < size; index++)
{
    cout<< setLength(montharray[index], 10) 
        <<  rainfallarray[index] <<endl;
}

// For the output like this:
Month       Rainfall (in)
---------------------
December       12
November       11
October        10
September      9
August         8
July           7
June           6
May            5
April          4
March          3
February       2
January        1

Or maybe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for( int index = 0; index < size; index++)
{
    cout<< setLength(montharray[index], 10, '.') 
        <<  rainfallarray[index] <<endl;
}

// For something more fancy:
Month       Rainfall (in)
---------------------
December.......12
November.......11
October........10
September......9
August.........8
July...........7
June...........6
May............5
April..........4
March..........3
February.......2
January........1
Last edited on
Thankyou Jockx! Thats the solution I was looking for!

I understand most of what you gave me but the " input.size()" you wrote on line 15 of your function eludes me. What exactly does that mean?? Sorry if I come off as ignorant or naive. I'm still very new to programming.
Last edited on
JockX wrote:
IO manipulators you are trying to use only have to do with representation of decimals.

No they don't.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void showarray(std::string montharray[], double rainfallarray[], int size)
{
    const std::string month_header = "       Month        ";
    const std::string rainfall_header = " (in)";

    const std::size_t month_width = month_header.size();
    const std::size_t rainfall_width = rainfall_header.size();

    std::cout << month_header << rainfall_header << '\n';
    std::cout << std::string(month_width + rainfall_width, '-') << '\n';

    // Uncomment the following if you prefer the "month.....amt" look.
    // std::cout << std::setfill('.');

    for (int index = 0; index < size; index++)
    {
        // left justify the month, right justify the rainfall amount.
        std::cout << std::left << std::setw(month_width) << montharray[index];
        std::cout << std::right << std::setw(rainfall_width) << rainfallarray[index] << '\n';
    }

    std::cout << '\n';
}
Topic archived. No new replies allowed.