我的获取热线无法得到答案

My getline couldn't get the answer

本文关键字:答案 获取 热线 我的      更新时间:2023-10-16
  1. 起初,一切都很好,直到我以正确的顺序添加 for 函数。
  2. 当我尝试反转它时,我正确pre_dates所有输出,但其他输出完全错误。
  3. 如何识别字符串中的单词?

我的产出(2012(:
平均最低温度 : 0 C
平均最高温度 : 0 C
总降雨量 : 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 <<"tt Clear       "<<ClearDays<<endl;
    cout <<"tt Rain        "<<RainyDays<<endl;
    cout <<"tt Snow        "<<SnowyDays<<endl;                        
    }               
}`

通常情况下,如果您使用的是重复的getline运算符,则可能需要清除cin缓冲区,因为它可能已从您之前的条目中遗留下来的数据

使用 :"cin.rdbuf((->in_avail((" 语句来解决此问题,看看它是否有效。