i have this assignment im doing and previously i did an assignment similar and it worked just fine. but now that im doing this second more complex form, its having trouble reading and storing the proper data. when i decided to try the previous source code that worked fine before i sumbitted it, it didnt work now either. im suppose to open a file and store a list of months names in an array of structs along with their high and low temps using a function outside of main, while also using 2 more functions to find the highest and lowest temp which will be output in function main. but with the data not coming out correctly i cant test the other 2 functions. i cant figure out if i have something corrupt somewhere or whats suddenly causing the files input on both programs to suddenly not work properly.
the input file is
January 47 36
February 51 37
March 57 39
April 62 43
May 69 48
June 73 52
July 82 56
August 81 57
September 75 52
October 64 46
November 52 41
December 45 35
the source code i got looks like
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct month
{
string monthName;
int highTemp;
int lowTemp;
};
void loadData(ifstream &infile, month Temperatures [], int &size);
month averageHigh(month Temperatures [], int size);
month averageLow(month Temperatures [], int size);
int main ()
{
ifstream inFile;
int length;
month Temperatures[12];
month highMonth, lowMonth;
cout << "This program is designed to take a struct of months and their corresponding \nhigh/low
temperatures "
<< "from an outside file, than calculate which months had the average high and low." <<
endl;
cout << endl;
inFile.open("weather.txt");
loadData(inFile, Temperatures, length);
averageHigh(Temperatures, length);
averageLow(Temperatures, length);
cout << highMonth.monthName << " has the highest temp of " << highMonth.highTemp << endl;
cout << lowMonth.monthName << " has the lowest temp of " << lowMonth.lowTemp << endl;
inFile.close();
return 0;
}
void loadData(ifstream &infile, month Temperatures [], int &size)
{
cout << "The months highs(left) and lows(right) are: " << endl;
for (size = 0; !infile.eof(); size++)
{
infile >> Temperatures[size].monthName >> Temperatures[size].highTemp >>
Temperatures[size].lowTemp;
cout << Temperatures[size].monthName << " " << Temperatures[size].highTemp << " " <<
Temperatures[size].lowTemp << endl;
}
}
month averageHigh(month Temperatures [], int size)
{
int highArray = 0;
for (int array = 1; array < size; array++)
if (Temperatures[highArray].highTemp < Temperatures[array].highTemp)
highArray = array;
return Temperatures[highArray];
}
month averageLow(month Temperatures [], int size)
{
int lowArray = 0;
for (int array = 1; array < size; array++)
if (Temperatures[lowArray].lowTemp < Temperatures[array].lowTemp)
lowArray = array;
return Temperatures[lowArray];
}
but with this the file keeps trying to store the values
4343069 0
0 0
11998488 0
321518481 32761
11993088 0
0 0
4342068 0
11998488 0
321518481 32761
4741664 0
0 0
4746688 0
then it gives random characters like G and pi and 0s.