Help! Need help assigning numbers to month

Basically I want to output the month and its rainfall. The thing is I have to do it in descending order. I already got the descending part down, but how would I pair it off to the corresponding month? Right now, it is outputing January-December instead of highest to lowest.
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
  #include <iostream>
#include <iomanip>
#include <string>

using namespace std;
struct Rainfall
{
	string month;
	double amount;
};
void userInput(Rainfall k[]);
void sortNumbers(Rainfall p[]);
void displayRainfall();


int main(){

	
	
	const int size = 12;
	Rainfall s[size];
	userInput(s);


	return 0;
}

void userInput( Rainfall k[]){
	cout << "Enter the rainfall(in inches) for January:" << endl;
	cin.ignore();
	getline(cin, k[0].month);
	cin >> k[0].amount;
	cout << "Enter the rainfall(in inches) for February : " << endl;
	cin.ignore();
	getline(cin, k[1].month);
	cin >> k[1].amount;
	cout << "Enter the rainfall(in inches) for March : " << endl;
	cin.ignore();
	getline(cin, k[2].month);
	cin >> k[2].amount;
	cout << "Enter the rainfall(in inches) for April : " << endl;
	cin.ignore();
	getline(cin, k[3].month);
	cin >> k[3].amount;
	cout << "Enter the rainfall(in inches) for May : " << endl;
	cin.ignore();
	getline(cin, k[4].month);
	cin >> k[4].amount;
	cout << "Enter the rainfall(in inches) for June : " << endl;
	cin.ignore();
	getline(cin, k[5].month);
	cin >> k[5].amount;
	cout << "Enter the rainfall(in inches) for July : " << endl;
	cin.ignore();
	getline(cin, k[6].month);
	cin >> k[6].amount;
	cout << "Enter the rainfall(in inches) for August : " << endl;
	cin.ignore();
	getline(cin, k[7].month);
	cin >> k[7].amount;
	cout << "Enter the rainfall(in inches) for September : " << endl;
	cin.ignore();
	getline(cin, k[8].month);
	cin >> k[8].amount;
	cout << "Enter the rainfall(in inches) for October : " << endl;
	cin.ignore();
	getline(cin, k[9].month);
	cin >> k[9].amount;
	cout << "Enter the rainfall(in inches) for November : " << endl;
	cin.ignore();
	getline(cin, k[10].month);
	cin >> k[10].amount;
	cout << "Enter the rainfall(in inches) for December :" << endl;
	cin.ignore();
	getline(cin, k[11].month);
	cin >> k[11].amount;

	sortNumbers(k);
}

void sortNumbers(Rainfall p[]){
	double temp;
	for (int i = 0; i < 12; i++){
		for (int j = i; j < 12; j++){
			if (p[i].amount < p[j].amount){
				temp = p[i].amount;
				p[i].amount = p[j].amount;
				p[j].amount = temp;
			}
		}

	}
	for (int j = 0; j < 12; j++){
		cout << p[j].month <<" "<<p[j].amount << endl;
	}
}
Heard of for-loops? You should give them a whirl. Even better range based for-loops:

1
2
3
4
5
6
7
void userInput( Rainfall k[]){
    for (Rainfall &rf : k) {
        cout << "Enter the rainfall(in inches) for " << rf.month << ": ";
	cin >> k[0].amount;
    }
    sortNumbers(k);
}


1
2
3
4
5
6
7
8
int main() {
    const int size = 12;
    Rainfall s[] = {
        {"January", -1}, {"February", -1}, ...
    };

    userInput(s);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void sortNumbers(Rainfall p[]) {
    Rainfall temp;
    for (int i = 0; i < 12; i++) {
        for (int j = i; j < 12; j++) {
	    if (p[i].amount < p[j].amount) {
	        temp = p[i];
		p[i] = p[j];
		p[j] = temp;
	    }
        }
    }

    for (int j = 0; j < 12; j++) {
        cout << p[j].month << " " << p[j].amount << endl;
    }
}
When you sort, you are swapping the rainfall amounts but not the associated months. Basically, it changes like this:

Rainfall for Millville, NJ during 2014
Jan 1.96           Jan 0.43
Feb 3.02           Feb 1.53
Mar 2.49           Mar 1.96
Apr 2.86           Apr 2.17
May 1.53           May 2.23
Jun 2.23    -->    Jun 2.49
Jul 2.17           Jul 2.86
Aug 9.52           Aug 3.02
Sep 3.06           Sep 3.06
Oct 0.43           Oct 3.94
Nov 3.94           Nov 4.93
Dec 4.93           Dec 9.52

Do you see what you've done?

You have to swap both the month name and the rainfall amount when you swap records.

Notice also that you repeat the same thing over and over on lines 29..36. This indicates a good point to add a loop.

29
30
31
    cout << "Enter the rainfall(in inches) for " << month_names[i] << " : " << endl;
    k[i].month = month_names[i];  // don't ask the user for this when you already specified the month he is answering for.
    cin >> k[i].amount;

The month_names array is easily given above:

14
15
16
17
const char* month_names[] =
{
  "January", "February", "March", ...
};


I have not analyzed your sort algorithm. Looks like an insertion sort.

Hope this helps.
Topic archived. No new replies allowed.