0

我正在尝试使用函数来处理 .dat 文件中的数据,但我似乎无法让函数正确读取数据。在第 62 行,它试图计算在三个不同高度记录的不同温度的低、高和平均值,但这些函数不处理任何数据。平均函数应该将数字的总数除以数字的计数,但它没有这样做。另外,我不能在工作中变得高或低。我究竟做错了什么?

#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>
#include <string>

using namespace std;

double qMeter = 0;
double hMeter = 0;
double oneMeter = 0;
int solDay = 0;
string garbage;
string localTime;
string decSol;
ifstream input;
ofstream output;

//function prototypes
double low(double lowTemp);
double high(double highTemp);
float average(double avgTemp);

int main()
{

input.open("curiosity234x.dat"); //opens input data file
output.open("output.dat"); //opens output data file


  for (int i = 0; i < 4; i++) //gets rid of the first four lines
{
  getline(input,garbage);
  cout << endl;
}

while (!input.eof())
{
    int count; 
    double newOneMeter;
    double newHMeter;
    double newQMeter;

    if (solDay == 2) //processes data for the second solar day
    {
     input >> solDay >> localTime >> decSol 
     >> newOneMeter >> newHMeter >> newQMeter;

     oneMeter = oneMeter + newOneMeter;
     hMeter = hMeter + newHMeter;
     qMeter = qMeter + newQMeter;
     count++;

     output << solDay << fixed << setprecision(1) << setw(5)
        << "Solar" << "Average" << "Low" << "High"
        << "Average" << "Low" << "High" 
        << "Average" << "Low" << "High" 
        << "Day" << "Temp" << "Temp" << "Temp" << "Temp" << "Temp"
        << "Temp" << "Temp" << "Temp" << "Temp"
        << fixed << setprecision(15) << "1 meter" << ".5 meters"
        << ".25 meters"
        << average(oneMeter) << low(oneMeter) << high(oneMeter)
        << average(hMeter) << low(hMeter) << high(hMeter)
        << average(qMeter) << low(qMeter) << high(qMeter);
    }

    if (solDay == 3) //processes data for the third solar day
    {
     input >> solDay >> localTime >> decSol 
     >> newOneMeter >> newHMeter >> newQMeter;

     oneMeter = oneMeter + newOneMeter;
     hMeter = hMeter + newHMeter;
     qMeter = qMeter + newQMeter;
     count++;

     output << solDay << fixed << setprecision(1) << setw(5)
        << "Solar" << "Average" << "Low" << "High" 
        << average(oneMeter) << low(oneMeter) << high(oneMeter)
        << average(hMeter) << low(hMeter) << high(hMeter)
        << average(qMeter) << low(qMeter) << high(qMeter);
    }
  }

  cout << endl << "The output.dat file has been written and transmitted.";
  /*
  reads first line. Assigns first string to 'int solDay' 
  second to 'string time', third to decSol, fourth to oneMeter, 
  fifth to hMeter and sixth to qmeter. Meters should have setw().
  */
getch();
return 0;

input.close();
output.close();
}


//functions used in main
double low(double lowTemp)
{
   int test = 10,000;
   double least;

   if (lowTemp < test)
   {
     lowTemp = test;
     lowTemp = least;       
    }
    return least;
}

double high(double highTemp)
{
   int test = 10,000;
   double most;

   if (highTemp < test)
   {
      highTemp = test;
      highTemp = most;        
    }
   return most;
}

float average(double avgTemp)
{
   avgTemp = avgTemp / count;
   return avgTemp;
}
4

1 回答 1

3

看一下low例如的实现:

int test = 10,000;
double least;

if (lowTemp < test)
{
  lowTemp = test;
  lowTemp = least;       
}
return least;

如果if块执行,您将未初始化的分配leastlowTemp. 然后不管它是否执行,你都会返回 uninitialized least

为什么你只分配testlowTemp然后用覆盖它least

也不10,000是代表一万的整数文字。您不能在文字中使用逗号来分隔数千。你必须写10000

于 2013-02-27T00:02:01.277 回答