正确使用功能

时间:2013-02-26 23:54:46

标签: c++

我正在尝试使用函数处理.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;
}

1 个答案:

答案 0 :(得分:3)

例如,查看low的实现:

int test = 10,000;
double least;

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

如果if块执行,则将未初始化的least分配给lowTemp。然后无论是否执行,都会返回未初始化的least

为什么要将test分配给lowTemp,然后才用least覆盖它?

10,000也不是表示一万的整数字面值。你不能在你的文字中加上逗号来分隔数千个。你必须写10000