如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?

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

本文关键字:存储 信息 结构 数组 函数 何修复 fstream 文件 输入      更新时间:2023-10-16

我有这个作业,以前我做了一个类似的作业,它工作得很好。 但是现在我正在做第二个更复杂的形式,它在读取和存储正确的数据时遇到了麻烦。 当我决定在总结之前尝试以前的源代码时,它现在也不起作用。 我假设打开一个文件并将月份名称列表存储在一个数组中结构以及它们的高温度和低温使用主函数外的函数,同时还使用另外 2 个函数来查找将在函数 Main 中输出的最高和最低温度。但是由于数据不正确,我无法测试其他 2 个功能。我无法弄清楚我是否在某处损坏了什么,或者是什么突然导致两个程序上的文件输入突然无法正常工作。

输入文件是

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

我得到的源代码看起来像

#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];                                                                                 
}

但是有了这个,文件一直试图存储值

4343069 0
0 0
11998488 0
321518481 32761
11993088 0
0 0
4342068 0
11998488 0 
321518481 32761
4741664 0
0 0
4746688 0

然后它给出随机字符,如 G 和 pi 以及 0。

这里有一个更干净的代码:

#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" : "tt") << 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;
}

天气.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

输出:

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