简体   繁体   中英

how do i fix my fstream file input in my function to store the proper info in my array of structs?

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.

Here is a cleaner code for you:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct Temperature {
    string month;
    int high;
    int low;
};

istream &operator>>(istream &in, Temperature &temp) {
    in >> temp.month >> temp.high >> temp.low;
    return in;
}

ostream &operator<<(ostream &out, const Temperature &temp) {
    out << temp.month << (temp.month.length() > 7 ? "\t" : "\t\t") << temp.high << '\t' << temp.low << endl;
    return out;
}

vector<Temperature> getData(ifstream &infile) {
    vector<Temperature> vect;
    Temperature temp;
    while (infile >> temp)
        vect.push_back(temp);
    return vect;
}

Temperature highest(vector<Temperature> &data) {
    return *max_element(data.begin(), data.end(), [](const Temperature &a, const Temperature &b) {
        return a.high < b.high;
    });
}

Temperature lowest(vector<Temperature> &data) {
    return *min_element(data.begin(), data.end(), [](const Temperature &a, const Temperature &b) {
        return a.low < b.low;
    });
}

int main() {
    ifstream infile("weather.txt");
    int length;
    auto data = getData(infile);
    for (auto &i : data)
        cout << i;
    auto h_T = highest(data), l_T = lowest(data);
    cout << endl
         << h_T.month << " has highest temperature of " << h_T.high << endl
         << l_T.month << " has lowest temperature of " << l_T.low << endl
         << endl;
    return 0;
}

weather.txt:

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

Output:

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

July has highest temperature of 82
December has lowest temperature of 35

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM