construct help

Can someone please show me how I would change the DailyTemps::load_temps() member function to a constructor? also how would I change the set_temp member function so that it can set either the high or low temperature for a given day? Thanks for the help!

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;


#define	DATA_FILE	  "./temps2.data"
#define MAX_DAYS	   366

class DailyTemps
{
public:
void	load_temps();
int	get_temp(int day);
void	set_temp(int day, int temp);
int	get_hi();
int	get_lo();
int	get_avg();

private:
ifstream temps_in;
int	 temps[MAX_DAYS];
int	 num_days;
};

int choose_function(int& day, int& temp);

int main()
{
DailyTemps	last_year;
int task, day, temp;

    last_year.load_temps();

    task = choose_function(day, temp);
    while ( task != 9 )
    {
	switch (task)
	{
	    case 1:	cout << "the highest high daily temperature last year was " << last_year.get_hi() << "\n";
			break;
	    case 2:	cout << "the lowest high  daily temperature last year was " << last_year.get_lo() << "\n";
			break;
	    case 3:	cout << "the  avg  high   daily temperature last year was " << last_year.get_avg() << "\n";
			break;
	    case 4:	cout << "the temperature for day " << day << " last year was " << last_year.get_temp(day) << "\n";
			break;
	    case 5:	last_year.set_temp(day, temp);
			cout << "the temperature for day " << day << " has been set to " << temp << "\n";
			break;
	    default:	cout << "please select from the menu choices\n\n";
	}
	task = choose_function(day, temp);
    }
    cout << "thank you for using the temperature database program\n\n";
    return(0);
}


int choose_function(int& day, int& temp)
{
int selection;


    cout << "\n\nplease select from the following:\n\t1) get high temperature\n\t2) get low temperature\n"
	 << "\t3) get average temperature\n\t4) get temperature for a specific day\n\t5) set the temperature "
	 << "for a specific day\n\n\t9) exit this program\n\nplease enter your selection: ";
    cin  >> selection;
    if ( (selection == 4) || (selection == 5) )
    {
	cout << "please enter the day desired: ";
	cin  >> day;
	if ( selection == 5 )
	{
	    cout << "please enter the correct temperature: ";
	    cin >> temp;
		
	}
    }
    return(selection);
}


void DailyTemps::load_temps()
{
    num_days = 0;
    temps_in.open(DATA_FILE);
    if ( temps_in.fail() )
    {
	cerr << "failed to open daily temperature list\n";
	exit(1);
    }
    while ( temps_in  >> temps[num_days] )  num_days++;
}


int  DailyTemps::get_temp(int day)
{
    return(temps[day-1]);
}


void DailyTemps::set_temp(int day, int temp)
{
    temps[day-1] = temp;
}


int DailyTemps::get_hi()
{
int hi_temp;

    hi_temp = temps[0];
    for (int i = 1; i < num_days; i++ )
    {
	if ( temps[i] > hi_temp ) hi_temp = temps[i];
    }
    return(hi_temp);
}


int DailyTemps::get_lo()
{
int lo_temp;

    lo_temp = temps[0];
    for (int i = 1; i < num_days; i++ )
    {
	if ( temps[i] < lo_temp ) lo_temp = temps[i];
    }
    return(lo_temp);
}


int DailyTemps::get_avg()
{
double avg_temp;
double total_sum;

total_sum = 0;

for(int i = 0; i < num_days; i++)
{
	total_sum += temps[i]; 
	
	avg_temp = total_sum / num_days;
}
    return(avg_temp);
}
Hint: You need to change the name of the function to something else and remove the return type.

-Albatross
Topic archived. No new replies allowed.