0

Hey all, I'm trying to output the coldest and hottest months and the corresponding temps from a text file which you'll see at the end. All I get is a blank space an a random number! Help pls!!! I've got one day to finish this up and the output isn't right >.<

I would put this simply, but the site is asking for more text, so here is the initial assignment.

Write a program that uses a struct and an array of structs to store the 12 months, and the high temperature and low temperature for each month. The program should read the Months’ names, and the temperatures from a given input file called temps.txt. The program should output the highest and lowest temperatures for the year, and the months that correspond to those temperatures. Your program MUST use the following functions: 1. Create a struct called Temperature with data members to store month, high temperature and low temperature. 2. Function loadData: The function reads and stores data in the array of struct from a text file (temps.txt). void loadData(ifstream &infile, Temperature [], int &size); 3. Function averageHigh: This function calculates and returns that record that has the high temperature. From that, you can print the high temperature and the corresponding month in main. Temperaure averageHigh(Temperature [], int size); 4. Function averageLow: This function calculates and returns that record that has the low temperature. From that, you can print the low temperature and the corresponding month in main. Temperaure averageLow(Temperature [], int size);

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <istream>

using namespace std;

//struct data type - Temperature
struct Temperature
{
    string month;
    int highTemp;
    int lowTemp;
};

//function prototypes
void loadData(ifstream &infile, Temperature[], int &size);
int averageHigh(Temperature[], int size);
int averageLow(Temperature[], int size);


int main()
{
    Temperature temps[12];
    ifstream infile("temps.txt");

    //variables
    int highest = 0;
    int lowest = 0;
    int index2 = 0;
    int index1 = 0;

    void loadData(ifstream &inFile, Temperature temps[], int &size);

    int averageHigh(Temperature temps[], int highest, int index1);
    cout << "The hottest month this year was " << temps[index1].month << " with a temperature of " << temps[index1].highTemp << endl;

    int averageLow(Temperature temps[], int lowest, int index2);
    cout << "The coldest month this year was " << temps[index2].month << " with a temperature of " << temps[index2].lowTemp << endl;

    //end program
    cin.ignore(100, '\n');
    cout << "Press any key to continue: " << endl;
    getchar();
    return 0;
}

void loadData(ifstream &inFile, Temperature temps[], int &size)
{
    while(!inFile.eof())
    {
        inFile >> temps[size].month >> temps[size].highTemp >> temps[size].lowTemp;
        size++;
    }
}

int averageHigh(Temperature temps[], int highest, int index1)
{
    for (int i = 0; i < 12; i++)
    {
        if (temps[i].highTemp > highest)
        {
            highest = temps[i].highTemp;
            index1 = i;
        }
        return index1;
    }
}

int averageLow(Temperature temps[], int lowest, int index2)
{

    for (int i = 0; i < 12; i++)
    {

        if (temps[i].lowTemp < lowest)
        {
            lowest = temps[i].lowTemp;
            index2 = i;
        }
        return index2;
    }
}
DashMGJ
  • 1
  • 1
  • You need to [get a couple of good beginners books to read](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). In the `main` function `void loadData(ifstream &inFile, Temperature temps[], int &size);` doesn't *call* the function `loadData`, it just *declares* it (again!). – Some programmer dude Dec 10 '17 at 00:09
  • I also suggest you read [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Some programmer dude Dec 10 '17 at 00:09

0 Answers0