- 首先,在我以正确的顺序添加for函数之前一切正常。
- 当我试图扭转它时,我正确地得到了所有pre_dates但是其他输出完全错误。
- 如何识别字符串中的单词?
醇>
我的输出(2012):
平均。最低温度:0℃
平均。最高温度:0℃
总降雨量:1.28
报告天气的天数如下:
清除1875655169
雨0
雪18
`string thedate, type, line;
int pre_date, ClearDays, SnowyDays, RainyDays;
double lowTemp = 0, highTemp = 0, totalrain = 0, avg_max = 0, avg_min = 0;
system("cls");
ifstream myfile("weather.txt");
if(!myfile.is_open())
{
cout <<"Error opening file.";
}
else
{
for(int i =0; i<4; i++)
{
while(myfile >> pre_date >> thedate >> type >> lowTemp
>> highTemp >> totalrain)
{
double avg_min = lowTemp++;
double avg_max = highTemp++;
getline(myfile, line);
if (line==" ") //error checking
{
cout << " Error" << endl;
ClearDays=0; //if not true
RainyDays=0;
SnowyDays=0;
}
else
{
ClearDays++;
RainyDays++;
SnowyDays++;
totalrain++;
}
}
cout <<"Year: " <<pre_date<< endl;
pre_date++;
cout <<"\t Avg. minimum temperature: " <<avg_min<<" C" <<endl;
cout <<"\t Avg. maximum temperature: " <<avg_max<<" C" <<endl;
cout <<"\t Total rainfall: " <<totalrain<< endl;
cout <<"\t Days where reported weather is: " << endl;
cout <<"\t\t Clear "<<ClearDays<<endl;
cout <<"\t\t Rain "<<RainyDays<<endl;
cout <<"\t\t Snow "<<SnowyDays<<endl;
}
}`
答案 0 :(得分:0)
通常情况下,如果您使用重复的getline运算符,您可能需要清除cin缓冲区,因为它可能遗留了您之前条目中的数据
使用:&#34; cin.rdbuf() - &gt; in_avail()&#34;声明来解决这个问题并查看它是否有效。