I'm not sure what I'm doing wrong so if you could take a look and give me some ideals it would be great. Thanks so much

Specifications: Write a program that uses a two-dimensional array to store the highest and lowest temperature for each month of the year. The program should output the average high, average low, and the highest and lowest temperatures, along with the corresponding month.

Data:
42.6 23.7
44.9 25.2
52.0 30.9
61.6 39.1
69.4 47.5
75.7 55.1
78.2 58.9
77.5 57.7
72.0 51.6
63.4 40.8
53.0 32.1
44.9 25.7

#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

const int numRows = 12;
const int numCols = 2;

void getData(ifstream& inputFile, double temps[][numCols], int numRows);
void averageHigh(double temps[][numCols], int numRows);
void averageLow(double temps[][numCols], int numRows);
void indexHighTemp(double temps[][numCols], int numRows);
void indexLowTemp(double temps[][numCols], int numRows);

int main() 
{
double temps[numRows][numCols] = {0.0};
 fstream infile;
 infile.open("Weather.txt");

 if (!infile)
 {
  cout << "Cannot open the input file. Program terminates!" 
    << endl;
    return 1;
 }
 while (infile)
 {
    getData(infile, temps, numRows);
    averageHigh(temps, numRows);
    averageLow(temps, numRows);
    indexHighTemp(temps, numRows);
    indexLowTemp(temps, numRows);
    cout << endl;
 }
    infile.close();

    return 0;
}

void getData(ifstream& inputFile, double temps[][numCols], int numRows)
{
    cout << fixed << showpoint << setprecision(1);
    double hiTemp, loTemp;
    string month;

    inputFile >> hiTemp >> loTemp;
    int rowIndex = 0;
    int colIndex;
    while (inputFile)
    {
        temps[rowIndex][0] = hiTemp;
        temps[rowIndex][1] = loTemp;
        rowIndex++;
        inputFile >> hiTemp >> loTemp;
    }
}

void averageHigh(double temps[][numCols], int numRows)
{
    double sum = 0;
    double avgHigh =0;

    for (int i=0; i<numRows; i++)
            sum += temps[i][0];
    avgHigh = (sum/numRows);
    cout<< "Average High was: "<< avgHigh << endl;

}

void averageLow(double temps[][numCols], int numRows)
{
    double sum = 0;
    double avgLow = 0;

    for (int i=0; i<numRows; i++)
            sum += temps[i][1];
    avgLow = (sum/numRows);

    cout << "Average Low was: "<< avgLow << endl;

}

void indexHighTemp(double temps[][numCols], int numRows)
{
    int ind = 0;
    double highest = temps[0][0];

    for (int i=1; i<numRows; i++)
            if (temps[i][0] > highest)
            {
                    highest = temps[i][0];
                    ind = i;
            }
    cout << "Highest temp of year was: " <<  ind << endl;

}

void indexLowTemp(double temps[][numCols], int numRows)
{
    int ind = 0;
    double lowest = temps[0][1];

    for (int i=1; i<numRows; i++)
            if (temps[i][1] < lowest)
            {
                    lowest = temps[i][1];
                    ind = i;
            }
    cout<< "Lowest temp of the year was: " << ind << endl;
}

Recommended Answers

All 3 Replies

Hi Callie C,

You didn't state what the problem is. Neither did you show the error messages you are getting.

But I guess if you have looked more closely, you would have seen in line 19 of your code that

fstream infile; should have been ifstream infile; Notice the i?

Edit:
Lastly, the question ask for the highest and lowest temperature not indexes.

2teez, Thank you so much that "i" made the program run like a cheetah. Also I'm sorry I didn't put enough info on the problem I will definitely add these things in future post. Again thank you.

You may like to know that this problem could nicely be handled using a struct and a C++ vector to hold all the struct's ...

struct Temps
{
    double high, low;
} ;

See example below ...

// takeInHighLowTemp.cpp //

/*
    The program should output the
    * average high,
    * average low,

    * and the highest
    * and lowest temperatures,
    * along with the corresponding month.
*/


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

const char* FNAME = "high_low_temps.txt";
/*
42.6 23.7
44.9 25.2
52.0 30.9
61.6 39.1
69.4 47.5
75.7 55.1
78.2 58.9
77.5 57.7
72.0 51.6
63.4 40.8
53.0 32.1
44.9 25.7
*/




struct Temps
{
    double high, low;

    istream& takeIn ( istream& is )
    {
        is >> high >> low;
        return is;
    }
} ;


bool load_from_file( const char* fname, vector< Temps >& vt )
{
    ifstream fin( fname );
    if( fin )
    {
        Temps ts;
        while( ts.takeIn( fin ) ) vt.push_back( ts );
        fin.close();
        return true;
    }
    // else if reach here ...
    cout << "There was a problem opening file: '" << fname
         << "'\n";
    return false;
}

void process_display( const vector< Temps >& vt )
{
    if( vt.size() )
    {
        double avg_low = vt[0].low, avg_high = vt[0].high;
        int m_low = 0, m_high = 0;

        for( size_t i = 1; i < vt.size(); ++ i )
        {
            if( vt[i].low < vt[m_low].low ) m_low = i;
            if( vt[i].high > vt[m_high].high ) m_high = i;
            avg_low += vt[i].low;
            avg_high += vt[i].high;
        }

        avg_low /= vt.size();
        avg_high /= vt.size();

        // now can output results ...
        cout << "Average low was  : " << avg_low << endl;
        cout << "Average high was : " << avg_high << endl;
        cout << "Lowest temp was: " << vt[m_low].low
             << " in month " << m_low+1 << endl;
        cout << "Highest temp was: " << vt[m_high].high
             << " in month " << m_high+1 << endl;
    }
}


int main()
{
    vector< Temps > vts;
    if( load_from_file( FNAME, vts ) )
    {
        cout << "The number of records was "
             << vts.size() << endl;

        process_display( vts );
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.