Trying to change data--not working? Help please

Hello, here is my code. I am trying to make it so that the I can set either the high or low temperature of a day through selection 5. Right now, my program prompts the user correctly but after I enter a new temperature to set as either the high or low for the day, when I go back through selection 4 to choose that day, I find that the data has not been changed (it still gives me the old temp). In other words my set functions are not sticking...any ideas why? 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;


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

class DailyTemps
{
public:
DailyTemps();
int	get_hi(int day);
int get_lo(int day);
float get_avg(int day);
void	set_temp_hi(int day, int temp);
void	set_temp_lo(int day, int temp);
int	get_hi();
int	get_lo();
float get_avg();

private:
ifstream temps_in;
int	 hi_temps[MAX_DAYS], lo_temps[MAX_DAYS];
int	 num_days;
};

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

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

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);

    task = choose_function(day, temp);
    while ( task != 9 )
    {
	switch (task)
	{
	    case 1:	cout << "The highest temperature last year was " << last_year.get_hi() << "\n";
			break;
	    case 2:	cout << "The lowest temperature last year was " << last_year.get_lo() << "\n";
			break;
	    case 3:	//cout moved to choose_function
			break;
	    case 4:	
			//cout moved to choose_function
			break;
	    case 5:	
			//cout moved to choose_function
			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)
{
DailyTemps last_year;
int selection;
char choice;

    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 == 3 )
	{
		cout << "would you like the average temperatures of the year or of a specific day? Type y for year and d for day:\n";
		cin  >> choice;
	}
	if (choice == 'y')
	{
		//cout << last_year.get_avg();
	}
	if (choice == 'd')
	{
		cout << "Enter the desired day: ";
		//cout << last_year.get_avg(day);
	}
    if ( (selection == 4) || (selection == 5) )
    {
		cout << "please enter the day desired: ";
		cin  >> day;
	}
	if (selection == 4) 
	{
		cout << "Would you like to get the high or low temp of the day? Type h for high or l for low:\n";
		cin >> choice;
		
		if( choice == 'h')
		{
			cout << last_year.get_hi(day) << " was the high temperature of the day.\n";
		}
		if( choice == 'l')
		{
			cout << last_year.get_lo(day) << " was the low temperature of the day.\n";
		}
	}

	if ( selection == 5 )
	{
		cout << "Would you like to set the high or low temperature of the day? Type h for high or l for low:\n";
		cin >> choice;
		cout << "please enter the correct temperature: ";
	    cin >> temp;
	
		if (choice == 'h')
			{
				last_year.set_temp_hi(day,temp);
				cout << temp << " set as the high temperature for day " << day;
		    }
		if (choice == 'l')
			{
		    	last_year.set_temp_lo(day,temp);
				cout << temp << " set as the low temperature for day " << day;
			}

	}
    
    return(selection);
}


DailyTemps::DailyTemps()
{
    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  >> hi_temps[num_days] >> lo_temps[num_days] )  num_days++;
}


int  DailyTemps::get_hi(int day)
{ 
	return(hi_temps[day-1]);
}

int  DailyTemps::get_lo(int day)
{
  return(lo_temps[day-1]);
}


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

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

int DailyTemps::get_hi()
{
int hi_temp;

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


int DailyTemps::get_lo()
{
int lo_temp;

    lo_temp = lo_temps[0];
    for (int i = 1; i < num_days; i++ )
    {
	if ( lo_temps[i] < lo_temp ) lo_temp = lo_temps[i];
    }
    return(lo_temp);
}
You are setting the temperatures of a class instance that is local to choose_function. When it goes out of scope, your changes are lost. Naming a local variable the same as a variable named in the calling function does not give you access to the one in the calling function.

So how do I fix this?
As cire said...

Change this
int choose_function(int& day, int& temp)

to
int choose_function(DailyTemps& last_year, int day, int temp)

You don't need day and temp as references unless you are changing their value... (which it doesn't look like you need to, other than locally)
This did not work, I think I do want to change their value. I want the temperatures to be permanently changed to what I set them as.
Topic archived. No new replies allowed.