rainfall stats

so ive been working on this code that will ask the user to input the amount of rainfall for each month and at the end it will display the total amount of rain for the year and the month that had the most and the least amount of rain also display the amount of rain that fell those months
Im having trouble displaying the stats. Right now i just have the total being displayed but its some weird number.
Anybody have some suggestion would be appreciated and yes using a class is required.
i have it in three files and header file and two source files.
header file

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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;


class Rainfall
{
	
private:
	double rain;
	double value;
	double total;
	double rainfall[12]; //holding the amount of rainfall
	int monthNum;
	double driestMonth = 0;     //max rainfall
	double wettestMonth = 0;    //min rainfall
public:

	Rainfall(double rainfall[], int num);
	double getTotal();   // get functions
	double getData();
	
};


source file rainfallStats.cpp
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
#include <iostream>
#include "stdafx.h"
#include "rain.h"
using namespace std;

Rainfall::Rainfall(double rainfall[], int num)
{
	rain = rainfall[12];
	monthNum = num;
	driestMonth;
	wettestMonth;
}
double Rainfall::getTotal()
{
	//getting the total rainfall count for the year
	for (int count = 0; count < 12; count++)
	{
		total += rainfall[count];
	}
	return total;
}

double Rainfall::getData()
{ 
	double tempMin = 0;			//place holder for min    
	double tempMax = 0;			//place holder for max
	wettestMonth = rainfall[0];
	driestMonth = rainfall[0];

	//find the lowest and highest number in the array
	for (int i = 0; i < 12; i++)
	{
		tempMin = rainfall[i];
		tempMax = rainfall[i];

		if (tempMin < driestMonth)
			driestMonth = tempMin;
		

		if (tempMax > wettestMonth)
			wettestMonth = tempMax;
		
	}

	return driestMonth, wettestMonth;
}


the main file
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

#include <iostream>
#include "stdafx.h"
#include "rain.h"

using namespace std;

int main()
{
	     
	double rain[12];	//puts users inputs into the array
	string months[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
	double monthDay[12];	//increment month number in the loop
	int monthNum = 0;


	// user inputs the rainfall amount for each month
	for (int i = 0; i < 12; i++)
	{
		cout << "Enter the rainfall (in inches) for month " << months[i] << ": ";
		cin >> rain[i];
		monthDay[i] = monthNum; //array increments in the loop and saves a new number for the monthNum
		
	}
	Rainfall raindata(rain, monthNum);	//saves info to the rainfall class

	//display total rainfall and the driest month and wettest month and the amount of rain that fall  those months

	
	cout << "-------------------------------------------------" << endl;
	cout << "Total rainfall: " << raindata.getTotal() << " inches." << endl;
	
    system("pause");
	return 0;
Last edited on
I rewrote your code changing he name of the class, so you can test mine for suggestions and safely keep yours.
rain.h:
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
#ifndef RAIN_H
#define RAIN_H

class Rain
{

private:
    double total {0};
    double* rainfall; //holding the amount of rainfall
    int driest_month_number {0};  // month of max rainfall
    double driest_month_rainfall {999999999};  // max rainfall
    int wettest_month_number {0}; //month of min rainfall
    double wettest_month_rainfall {0}; //min rainfall

public:
    Rain() = default;
    Rain(double rainfall_per_month[]);

    void calculateTotal();
    void calculateWettestDriest();

    // get functions
    double getTotal() const {return total;}
    int getDriestMonthNumber() const {return driest_month_number;}
    int getWettestMonthNumber() const {return wettest_month_number;}
    double getDriestMonthRainfall() const {return driest_month_rainfall;}
    double getWettestMonthRainfall() const {return wettest_month_rainfall;}
};

#endif // RAIN_H 


Rain.cpp
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
#include <iostream>
#include "rain.h"

using std::cin;
using std::cout;
using std::endl;

Rain::Rain(double rainfall_per_month[]) :
    rainfall{rainfall_per_month}
{}

void Rain::calculateTotal()
{
    //getting the total rainfall count for the year
    for (int count = 0; count < 12; count++)
        total += rainfall[count];
}

void rain::calculateWettestDriest()
{
    driest_month_number = 0;
    // We need a high number, otherwise it will always
    // be less than fallen rainfall
    driest_month_rainfall = 999999999;
    wettest_month_number = 0;
    wettest_month_rainfall = 0;

    //find the lowest and highest number in the array
    for (int i = 0; i < 12; i++)
    {
        if (rainfall[i] < driest_month_rainfall) {
            driest_month_rainfall = rainfall[i];
            driest_month_number = i;
        }

        if (rainfall[i] > wettest_month_rainfall) {
            wettest_month_rainfall = rainfall[i];
            wettest_month_number = i;
        }

        // And  what if two months have the same amount of rainfall? ;-)
    }
}


main.cpp:
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
#include <iostream>
#include <limits>
#include <string>
#include "rain.h"

using std::cin;
using std::cout;
using std::endl;

const int MONTHS = 12;

void pause();

int main()
{
    std::string months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                             "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

    // user inputs the rainfall amount for each month
    double rain[MONTHS];	//puts users inputs into the array
    for (int i = 0; i < (MONTHS); i++)
    {
        cout << "Enter the rainfall (in inches) for month "
             << months[i] << ": ";
        cin >> rain[i];
    }

    Rain raindata(rain);	//saves info to the rainfall class

    // display:
    // - total rainfall
    // - driest month
    // - wettest month
    // - the amount of rain that fall those months
    cout << "\n-------------------------------------------------" << endl;
    raindata.calculateTotal();
    cout << "Total rainfall: " << raindata.getTotal() << " inches." << endl;
    raindata.calculateWettestDriest();
    cout << "Driest month was: " << months[raindata.getDriestMonthNumber()] << endl;
    cout << "Wettest month was: " << months[raindata.getWettestMonthNumber()] << endl;
    cout << "In the driest month fell " << raindata.getDriestMonthRainfall()
         << " inches of rainfall." << endl;
    cout << "In the wettest month fell " << raindata.getWettestMonthRainfall()
         << " inches of rainfall." << endl;

    pause();
    return 0;
}

void pause()
{
    std::cout << "Press ENTER to continue..." << std::endl;
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}

Topic archived. No new replies allowed.