array of structs help

closed account (1Ck93TCk)
Hi,

I searched and found this thread http://www.cplusplus.com/forum/beginner/226483/

with almost the exact same program that I'm working on, and it answered some of my questions, but I still need some things cleared up for me.

I have an array of structs, and in each struct are 3 things - month, high temp, low temp. The program is supposed to find the highest and lowest and output that with the corresponding month.

I'm having trouble with:

1. When I run this, I get the correct high and low temperature, but it just outputs January for the month in every case.

2. I haven't been able to create a return statement for findHigh and findLow. Everything I've tried creates an error. I know functions can return type struct, but maybe I'm just not getting the right syntax?

return Temperature;
return temps[];
return temps[highest].high;
?? I've tried these, but they're no good.

The function prototypes are provided as part of the assignment and cannot be changed.

I appreciate any help!
jmb


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int MAX = 12;

struct Temperature
{
    string month;
    int high;
    int low;
};

int loadData(ifstream &inFile, Temperature temps[], int &rows);
Temperature findHigh(Temperature temps[], int rows, int &highest);
Temperature findLow(Temperature temps[], int rows, int &lowest);

int main()
{
    Temperature temps[MAX];
    ifstream inFile;
    int rows = 0;
    int j = 0;
    int highest = 0;
    int lowest = 0;

    loadData(inFile, temps, rows);
    
    for (j = 0; j < rows; j++)
    {
        cout << temps[j].month << " "
             << temps[j].high << " "
             << temps[j].low << endl;
    }

    findHigh(temps, rows, highest);
    cout << "The high temp was " << temps[highest].high << " in " 
         << temps[highest].month << endl;

    findLow(temps, rows, lowest);
    cout << "The low temp was " << temps[lowest].low << " in " 
         << temps[lowest].month << endl;

    return 0;
}
int loadData(ifstream &inFile, Temperature temps[], int &rows)
{
    int i;
    
    inFile.open("temps.txt");

    for (i = 0; i < MAX; i++)
    {
        inFile >> temps[i].month >> temps[i].high >> temps[i].low;
        rows++;
    }
    cout << "File open" << endl;
    cout << rows << " rows counted." << endl;

}
Temperature findHigh(Temperature temps[], int rows, int &highest)
{

    int index = 0;

    for (index = 0; index < rows; index ++)
    {
        if (temps[index].high > temps[highest].high)
            temps[highest].high = temps[index].high;
    }

}
Temperature findLow(Temperature temps[], int rows, int &lowest)
{
    cout << "findLow called" << endl;
    int index = 0;

    for (index = 0; index < rows; index++)
    {
        if (temps[lowest].low > temps[index].low)
            temps[lowest].low = temps[index].low;
    }
}


Here's the input file:
1
2
3
4
5
6
7
8
9
10
11
12
January 47 36
February 51 37
March 57  39
April 62 43
May 69 48
June 73 52
July 81 56
August 83 57
September 81 52
October 64 46
November 52 41
December 45 35


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int MAX = 12;

struct Temperature
{
    string month;
    int high;
    int low;
};

int loadData(Temperature temps[]);
int findHigh(Temperature temps[], int rows);
int findLow(Temperature temps[], int rows);

int main()
{
    Temperature temps[MAX];
    int rows = loadData(temps);

    for (int i = 0; i < rows; i++)
        cout << temps[i].month << " "
             << temps[i].high << " "
             << temps[i].low << endl;

    int highest = findHigh(temps, rows);
    cout << "The high temp was "
         << temps[highest].high  << " in " 
         << temps[highest].month << endl;

    int lowest = findLow(temps, rows);
    cout << "The low temp was "
         << temps[lowest].low   << " in " 
         << temps[lowest].month << endl;

    return 0;
}

int loadData(Temperature temps[])
{
    ifstream inFile("temps.txt");

    int i = 0;
    for ( ; i < MAX; i++)
        inFile >> temps[i].month >> temps[i].high >> temps[i].low;

    cout << "File open" << endl;
    cout << i << " rows counted." << endl;
    return i;
}

int findHigh(Temperature temps[], int rows)
{
    int highest = 0;
    for (int i = 1; i < rows; i++)
        if (temps[i].high > temps[highest].high)
            highest = i;
    return highest;
}

int findLow(Temperature temps[], int rows)
{
    int lowest = 0;
    for (int i = 0; i < rows; i++)
        if (temps[lowest].low > temps[i].low)
            lowest = i;
    return lowest;
}

closed account (1Ck93TCk)
Wow, thank you!

I'm wondering about the findLow/findHigh function types. The prototypes that we were given for the assignment gave Temperature as the function type. We're not supposed to change the prototypes, but I'm wondering if maybe I misunderstood the requirements. Is it even possible to use Temperature as the type in this way, or do I need it to be int in order to return a value?

Thank you for the help!

jmb
Topic archived. No new replies allowed.