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!
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
usingnamespace 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);
}