我刚刚开始编程,不知道很多,但是基本上我必须写这个程序:
Write a program called WeatherStats which will read a RAWS weather file and will return the following statistics:
Range of dates in the file
Average high temperature
Average low temperature
Maximum high temperature (include date)
Minimum low temperature (include date)
Total rainfall
How many days it rained
我知道如何读取文件并分配所需的变量,但各行并不相同,这是我到目前为止的内容:
if (weatherStats.is_open())
{
do
{
weatherStats >> month >> day >> precip >> hour1 >> hour2 >> temp1 >> temp2 >> hum1 >> hum2 >> elevation >> precipdur1 >> precipdur2;
cout << month << " " << day << " " << precip << " " << hour1 << " " << hour2 << " " << temp1 << " " << temp2 << " " << hum1 << " " << hum2 << " " << elevation << " " << precipdur1 << " " << precipdur2;
cout << endl;
} while (!weatherStats.eof());
weatherStats.close();
}
system("PAUSE");
}
我的问题是,文件中的每一行都没有相同数量的数字,因此当我从文件中读取它们到变量时,第一行之后它们得到的数字是错误的。
我为此一直在苦苦挣扎,希望能对您有所帮助。
这是文件:
格式:
1. Month
2. Day
3. Precipitation – the daily rain amount specified in hundredths of an inch or millimeters (integer)
4. Hour 1 – the hour at which the minimum temperature was recorded (0-2400)
5. Hour 2 – the hour at which the maximum temperature was recorded (0-2400)
6. Temperature 1 – minimum temperature in degrees Fahrenheit or Celsius (integer)
7. Temperature 2 – maximum temperature in degrees Fahrenheit or Celsius (integer)
8. Humidity 1 – maximum humidity in percent, 0 to 99 (integer)
9. Humidity 2 – minimum humidity in percent, 0 to 99 (integer)
10. Elevation - feet or meters above sea level
11. Precipitation Duration (optional) - the beginning and ending times (0-2400)
7 1 16 2300 1400 50 80 99 26 4570 1700 2000
7 2 0 500 1600 46 84 99 24 4570
7 3 11 400 1500 50 88 99 24 4570 1700 1800
7 4 0 600 1600 54 85 63 28 4570
7 5 0 500 1600 50 76 86 31 4570
7 6 0 500 1600 44 82 83 23 4570
7 7 0 500 1500 43 83 76 14 4570
7 8 0 500 1800 42 84 67 18 4570
7 9 0 500 1600 43 88 69 12 4570
7 10 0 400 1600 46 87 59 14 4570
7 11 0 600 1600 43 76 29 8 4570
7 12 0 400 1700 36 84 51 10 4570
7 13 0 500 1600 39 87 45 10 4570
7 14 0 600 1700 42 86 53 8 4570
7 15 0 200 1500 51 83 44 18 4570
7 16 10 400 1300 50 81 58 22 4570 1100 1200
7 17 0 600 1700 44 84 82 15 4570
7 18 1 500 1200 46 83 77 19 4570 500 600
7 19 0 500 1700 41 87 76 11 4570
最佳答案
您可以使用stringstream
。
首先将默认值存储在所有int变量中
然后从文件流中读取一行到字符串变量
然后通过stringstream
将该字符串放入所有int变量中。
以下是示例代码:(根据您的修改)
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main (){
//....
//....
if (weatherStats.is_open()){
string str="";
do{
month=0;day=0;precip=0;hour1=0;hour2=0;temp1=0;temp2=0;hum1=0;
hum2=0;elevation=0;precipdur1=0;precipdur2=0;
//put default values in all variable in each loop
//NOTE: as I can see in your input file only last two is missing, SO you can here set default values to last two variable only, not for all the variables
str=weatherStats.ReadLine();
//read line in to string
// then put all those in your all variables
stringstream(str) >> month >> day >> precip >> hour1 >> hour2 >> temp1 >> temp2 >> hum1 >> hum2 >> elevation >> precipdur1 >> precipdur2;
cout << month << " " << day << " " << precip << " " << hour1 << " " << hour2 << " " << temp1 << " " << temp2 << " " << hum1 << " " << hum2 << " " << elevation << " " << precipdur1 << " " << precipdur2;
cout << endl;
} while (!weatherStats.eof());
weatherStats.close();
}
//.......
}
检查this page - stringstream的最后部分以获取帮助。
在这里,如果字符串包含的数字较少,则不会更改其他变量,因此其默认值将保持不变。