Question

I'm having trouble getting this program to output things in the correct format. The setw function doesn't seem to do what I want it to do. Also, the averages, lows and highs are giving me the wrong answer. The program is supposed to read in a file and process them to find the highest temp, the lowest temp and the average of all temps for that day. Each day has the same values as the previous day.

Can someone point out what I'm doing wrong?

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

double averageTemp(double total, int number);
double lowTemp(double valueLow);
double highTemp(double valueHigh);
double high = 0;
double low = 0;

int main()
{
    std::string dummy = "";
    int day, time = 0;

    double input1[10][50], input5[10][50], input25[10][50];
    /*
    Keeps track of the data:
        Dimension 1: stores the input for each time of the day.
    */

    double m1[10][3], m5[10][3], m25[10][3];
    /*
    Process data: 
        Dimension 1:
            Index 1: Keeps track of day
        Dimension 2:
            Index 1: Average temp
            Index 2: Low temp
             Index 3: High temp
    */

   std::ifstream datain;
   std::ofstream dataout;

    datain.open("curiosity234X.dat");
    dataout.open("output.dat");

    for (unsigned int i = 0; i < 4; i++)
    {
        getline(datain, dummy);
    }

    int dayCount = 0, loopDay;

    while (datain >> day >> dummy >> dummy >> input1[day][time] 
                  >> input5[day][time] >> input25[day][time])
    {
                  while (dayCount == 0)
                  {
                        loopDay = day - 1;
                        dayCount++;
                  }

     m1[day][0] = averageTemp(input1[day][time], time);
     m1[day][1] = lowTemp(input1[day][time]);
     m1[day][2] = highTemp(input1[day][time]);

     m5[day][0] = averageTemp(input5[day][time], time);
     m5[day][1] = lowTemp(input5[day][time]);
     m5[day][2] = highTemp(input5[day][time]);

     m25[day][0] = averageTemp(input25[day][time], time);
     m25[day][1] = lowTemp(input25[day][time]);
     m25[day][2] = highTemp(input25[day][time]);

     time++;
    }

    dataout << std::setw(5) << std::fixed;
    dataout << "Average" << "Low" << "High" << std::endl 
            << "Temp" << "Temp" << "Temp" <<std::endl
            << "1 meter" << ".5 meters" << ".25 meters" << std::endl;

    std::cout << std::setw(5) << std::fixed;
    std::cout << "Average" << "Low" << "High" << std::endl 
              << "Temp" << "Temp" << "Temp" <<std::endl
             << "1 meter" << ".5 meters" << ".25 meters" << std::endl;

    for (unsigned int i = loopDay; i < day; i++)
    {
        dataout << std::fixed << std::setprecision(1) << std::setw(5);
        dataout
                << m1[day][0] << m1[day][1] << m1[day][2]
            << m5[day][0] << m5[day][1] << m5[day][2]
                << m25[day][0] << m25[day][1] << m25[day][2] 
                << std::endl;

        std::cout << std::fixed << std::setprecision(1) << std::setw(5);
        std::cout
                  << m1[day][0] << m1[day][1] << m1[day][2]
          << m5[day][0] << m5[day][1] << m5[day][2]
              << m25[day][0] << m25[day][1] << m25[day][2] 
                  << std::endl;
    }

    std::cout << std::endl << std::endl << std::endl
              << "The output.dat file has been written and transmitted."
              << std::endl;

    _getch();
    return 0;
}

double lowTemp(double valueLow)
{
    if (valueLow < low)
    {
        ::low = valueLow;

        return valueLow;
    } else 
    {
        return low;
    }
}

double highTemp(double valueHigh)
{ 
    if (valueHigh > high)
    {
        ::high = valueHigh;

        return valueHigh;
    } else 
    {
        return high;
    }
}

double averageTemp(double total, int number)
{
    double average = total/double(number);

    return average;
}

No correct solution

OTHER TIPS

  • Please check this loop:
    for (unsigned int i = loopDay; i < day; i++) { ... }

    the i variable isn't used inside the loop body, so you're doing the same thing n-times. I think you should change m1[day][...] with m1[i][...] (same for m5 and m25).

  • About the loopDay variable:

    while (dayCount == 0)
    {
      loopDay = day - 1;
      dayCount++;
    }
    

    I suppose what you mean is:

    if (dayCount == 0)
      loopDay = day;
    
    ++dayCount;
    

    In this way loopDay stores the first day seen (the first day you'll print in the for loop). Considering that dayCount isn't referenced in other points of the source code you could get rid of it and write:

    if (time == 0)
      loopDay = day;
    
  • Now you have to change the the upper limit in the for loop: the last day you have seen must be included so < day must be <= day.

There are other things to change but this is a starting point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top