【发布时间】:2016-08-19 10:55:51
【问题描述】:
该文件是一个名为 TotalMonthlyRainfall2014.txt 的文本文件,它在一行中包含以下内容:
0.33 0.41 1.45 1.74 3.40 3.26 0.98 4.34 0.06 2.09 2.13 1.13
我想从文件中读取数字并将它们存储到一个名为monthRain 的数组中。所以 monthRain[0] 将为 0.33,monthRain[1] 将为 0.41,依此类推。
这是我目前所拥有的:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
//global variable
const int months = 12;
const string FILENAME = "TotalMonthlyRainfall2014.txt";
int main()
{
ifstream inFile; //input file stream
float monthRain[months];
//open the file
inFile.open(fileName.c_str());
//loop through and get data from file
for (int i = 0; i < months && (inFile >> monthRain); i++)
{
cout << setprecision(2) << fixed << showpoint << monthRain[i];
}
inFile.close();
}
我想问题是如何将数字正确存储到数组monthRain中。
【问题讨论】:
-
你对这段代码有什么特别的问题?
-
您只是忽略了要求 mcve 的 cmets 并再次发布了完全相同的问题。为什么?
-
我试图清理问题的外观。这是我第一次发帖,所以我什至不确定如何正确格式化问题。我不确定 mcve 是什么。