Classes with an array

my header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
const int maxMonth =12;
class Stats 
{
public:
	Stats(); //default constructor
	Stats(double);
        //...
	void showdata();
	//function prototypes
	void setValues(int,double[]);

private:
	double Values[maxMonth];

};

and the 2 .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 "Stats.h"

Stats::Stats()	//default constructor
{
for (int i=0;i<maxMonth;i++)
	{
	Values[i]=0;	//set all array in values to 0
	}
}
Stats::Stats(double nValues) //another constructor
{
	for (int i=0;i<maxMonth;i++)
	{
	Values[i]=nValues;
	}
}

void Stats::showdata()
{
	cout<<"Month    :";
	for (int i=1;i<=maxMonth;i++)
	{
		cout<<i<<"  ";
	}
	cout<<"\nRainfall :";
	for (int i=0;i<maxMonth;i++)
	{
		cout<< Values[i]<<"  ";
	}
	cout<<endl;
        //....
}

void Stats::setValues(int month, double nValues[maxMonth ])
{
	for (int i=0;i<d;i++)
	{
		cout<<"Enter the amount of rainfall for Month #"<<(i+1)<<" ";
		cin>>nValues[i];
		if (nValues[i]<0)
			nValues[i]=0;
	}
}	

1
2
3
4
5
6
7
8
9
10
11
12
#include "Stats.h"

int main()
{	
	Stats rainfall;

	rainfall.setValues(a,b); //2 arguments here
	cout<<endl;

	system("Pause");
	return 0;
}

my setValue is to accept 2 arguments, the first one being an integer that indicates which value is being provided (from 1-12) and the second is a double that will hold the data value.
im asked to use object name rainfall and call setValue member function to set each of the month rainfall to a user entered amount.
my current problem is that im able to allow the user to enter 12 number, one for each month WITHOUT setting setValue to accept 2 args.
but when i set setvalue to accept 2 args, i got problems in implementing it.
i tried to change setvalue to something like this:
1
2
3
4
5
6
7
8
9
10
void Stats::setValues(int month, double nValues[maxMonth])
{
	for (int i=0;i<month;i++)
	{
		cout<<"Enter the amount of rainfall for Month #"<<(i+1)<<" ";
		cin>>nValues[i];
		if (nValues[i]<0)
			nValues[i]=0;
	}
}
where as the 1st arg is changed to 12
rainfall.setValues(12,b);. here i dont know what to set my 2nd arg to so that the program will work correctly. set b to any number and it will run but then crash...
I think your function should look more like this:

void Stats::setValue(int month, double value) { /*...*/ }

What I mean is that each call to setValue should only set the value for one month. The last for loop in your post should probably be inside main, and setValue should probably be called inside it.
so in the 2nd .cpp, where i call rainfall.setValues(a,b), i can calls it 12 times, where a=1, a=2,...a=12. then the user will enter a number for each.
and i can remove the for loop inside void Stats::setValue(int month, double value)
but the problem is that i still a need an array or pointer type. if i set the double to double * or double [] , then when i run it, it will simply crash...
i need to set b to something, where b is a double that hold the actual data value of the user input. which i set b to 0 in rainfall.setValues(a,0) so when setvalues is call upon, the default is zero and it would still require the user to enter a number.
so can someone helps me with how do i make it so that the 2nd arguement of setValues will accept the input of 0, or possibly something else?

edit: forgot to mention that setvalue cant be inside main, only in the 1st .cpp where the function defintion is located
Last edited on
dznguy wrote:
i still a need an array

The array you'll use inside your setValue function
is the Values array (the data member of your Stats class).

dznguy wrote:
setvalue cant be inside main

As long as you include Stats.h in main and the setValue
function declaration is inside Stats.h, you can call it from main.

It should look like this:

Stats.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "Stats.h"

//...

void Stats::setValue(int i, double v)
{
    //set the i-th element of your
    //Stats::Values array to v
}

//... 

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "Stats.h"

//...

int main()
{
    Stats rainfall;

    for (int i=0; i<12; i++)
    {
        double value;

        //get value for i-th month

        rainfall.setValue(i,value);
    }

    system("pause");
    return 0;
}

//... 
Last edited on
Topic archived. No new replies allowed.