如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
how do i fix my fstream file input in my function to store the proper info in my array of structs?
我有这个作业,以前我做了一个类似的作业,它工作得很好。 但是现在我正在做第二个更复杂的形式,它在读取和存储正确的数据时遇到了麻烦。 当我决定在总结之前尝试以前的源代码时,它现在也不起作用。 我假设打开一个文件并将月份名称列表存储在一个数组中结构以及它们的高温度和低温使用主函数外的函数,同时还使用另外 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
相关文章:
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- 按字母顺序打印存储在字符数组中的信息
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- 对于存储另一个类所需信息的类,例如其构造,是否有设计模式?
- structtm是否将时区信息存储为其数据成员
- 将不同的模板化类存储在一个容器中,而不会丢失有关其类型的信息
- 合法的方式将destructor信息存储到void*
- 为什么类型转换对象不会更改其地址?有关对象类型的信息存储在哪里?
- 为什么C++不使用集中存储类型信息以实现高效的 RTTI
- (C++) 如何从要存储在结构数组中的二进制文件中读取.dat信息?
- 我应该如何存储从文件中获取的这些信息?
- 在TXT文档的一部分中查找信息并将其存储在变量中
- 如何将循环中的信息存储到数组中
- 信息实际上并未存储在节点阵列中
- DAWG可以用于存储与单词相关的信息吗?
- 获取存储在矢量结构中的信息时出现问题
- 如何在派生类中存储基类初始化期间获得的信息
- 使用 fstream 对象将文件中的信息存储到变量中
- 在 Windows 上存储信息有哪些选项?我应该如何阅读此信息
- QImage与Format_Mono如何存储信息