no matching function call

On line 68 I keep getting an error that says "No matching function for call to 'getHighest'. I have googled and found other people with the same assignment and looked at their code and I still don't see what I'm doing wrong.

All my other functions are working correctly and this one seems to be set up the same.

What am I missing?

Also I don't fully understand what the "&" means on the 3rd variable on both the getHighest and getLowest function. Is that what is messing me up? It has to stay in for this assignment.


Thanks

Adam
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
 //
//  main.cpp
//  Week 7 Assignment 1
//
/*
    This program is to calculate total rainfall 
    Average rainfall
    Highest month of rainfall
    Lowest month of rainfall
    
    Ask user for rainfall per month
    Use a loop to capture data for each month
    Store data in an array
*/

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

//Function Prototypes

double getTotal(double [ ], int);
double getAverage(double [ ], int);
double getHighest(double [ ], int, int&);
double getLowest(double [], int, int&);
const int SIZE = 12;





int main()
{
    
    string months[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
    double rainfall[SIZE];
    double total,highest, lowest;
    double average;
    
    //Ask for total rainfall
    
    cout << "Please enter the rainfall for each month of the year\n";
    
    // Loop to gather all months
    
    for (int count =0; count <SIZE; count++)
    {
        cout << months[count] << ":";
        cin >> rainfall[count];
        while (rainfall[count]<0)
        {   cout << "Please enter a valid number\n";
            cout << months[count] << ":";
            cin >> rainfall[count];
        }
    }
    
    //Get the total rainfall amounts
   
    total = getTotal(rainfall, SIZE);
    cout << "The total rainfall for the year is " << total << endl;
    
    //Get average rainfall for each month
    
    average = getAverage(rainfall, SIZE);
    cout << "The average rainfall per month is " << average << endl;
    
    highest = getHighest(rainfall, SIZE, &SIZE);
    cout << "The month with the highest rainfall is " << highest;
    return 0;
}

double getTotal(double rainfall[], int SIZE)
{
    double total = 0;
    
    for (int count =0; count < SIZE; count++)
    {
        total +=rainfall[count];
    }
    
    return total;
}

double getAverage(double rainfall[], int SIZE)
{
    double average;
    double total;
    total = getTotal(rainfall, SIZE);
    average = total/SIZE;
    
    return average;
}



double getHighest(double rainfall[], int SIZE, int &)
{
    
    double highest;
    
    highest =rainfall[0];
    
    for (int count = 0; count < SIZE; count++)
    {
        if (rainfall[count] > highest)
        {
            highest = rainfall[count];
        }
    }
    
    return highest;
    
    }
    


There are two problems with your getHighest method declaration. First, your third argument should be a reference, not a pointer, so you must not pass the address of the constant integer SIZE to the function. You should use

highest = getHighest(rainfall, SIZE, SIZE);

Second, you have a nonconstant reference to a const integer in the method, which is not allowed, since constant defined objects cannot be changed later. So you should declare your function as

double getHighest(double [ ], int, const int&);

and make sure that you are not going to change the value of SIZE later.
Last edited on
The question is,what is the purpose of each of the three parameters in this declaration:
 
double getHighest(double [ ], int, int&);

Because I've seen this same assignment come up here more than once previously, I can guess. The first two you filled in, the array, and the size of that array. The final parameter I guess is supposed to be used to return the subscript (or index) of the array element which had the highest rainfall.

Thus it might look like this:
double getHighest(double rainfall[], int SIZE, int & highIndex)

and you would call the function like this:
1
2
    int highIndex;
    double highest = getHighest(rainfall, SIZE, highIndex);

all that remains is to add the code inside the function to place the correct value into highIndex, plus adding the code to make use of that value after the function has returned. (I believe, from memory its purpose is to allow the printing of the name of the month (January - December) which had that rainfall value).

You should also read up on passing by value versus passing by reference in the tutorial:
http://www.cplusplus.com/doc/tutorial/functions/

Thank you for the replies.

Chervil,

You are correct in what the third value should be. It should be reading what month had the high and outputting it.

Would it look like highIndex = rainfall[count] ?

I'm pretty confused on what variable the month is even assigned to. Do I need to put another string variable inside this function with the month names and make a for loop specifically for the highIndex to out put the string?

Thanks
Would it look like highIndex = rainfall[count] ?

One way to answer this question is to consider what are the types of each expression or variable.

highIndex
is an int
rainfall[]
is an array of type double
count
is of type int and finally
rainfall[count]
is of type double

... so you were asking whether a value of type double should be stored in an variable of type int. Of course that's possible, but it should give an uneasy feeling as it involves discarding the fractional part of the value.

The other way to answer the question is to consider what one is trying to achieve. This was my description: "The final parameter I guess is supposed to be used to return the subscript (or index) of the array element which had the highest rainfall."
Do you understand the term subscript in this context?

I'm pretty confused on what variable the month is even assigned to. Do I need to put another string variable inside this function with the month names and make a for loop specifically for the highIndex to output the string?

No, that's making things much more difficult than necessary. No more variables are needed, And definitely no more loops. Let's just keep things as simple as we can.

Look again at your main() function. Do you see this at line 36:
36
37
    string months[] = {"January","February","March","April","May","June",
        "July","August","September","October","November","December"};

With the help of the highIndex value, you can use this array of month names, still keeping within the main() function.
The code might look like this:
70
71
    cout << "The month of " << months[highIndex] 
         << " had the highest rainfall." << endl;
(use whatever wording you like, this is only an example).

I have to point out again - do you understand the difference between passing a parameter by value and passing by reference?


Last edited on
Yes, I think I understand the difference between reference and value. So my 3rd variable I am passing by reference and that is shown by the ampersand right? If I'm trying to pass the string [months] variable how do I do that into the int& and keep it in the right format?

Also the subscript returns the element of the array (i.e. Month). How does the highIndex variable know which month we are looking at? How do I get it to look at the count from the for loop that comes right before it? Should it be part of that loop in some way?

Thanks for your help
Ok, some of your questions I don't quite understand,
If I'm trying to pass the string [months] variable how do I do that into the int& and keep it in the right format?
I can only say, briefly, that is not what you should be trying to do.

The int & parameter I think you correctly understand is passed by reference. What that means is that the variable is defined in main() as an ordinary integer. The reason it's used is because in the ordinary way, a function can return only one value, and the function is already using that to return the actual rainfall value. So this integer passed by reference becomes a second value returned from the function, in effect.

How do I get it to look at the count from the for loop that comes right before it? Should it be part of that loop in some way?


Sorry, I was mistaken, I thought I'd explained this, but I was thinking of the answer I gave to another question from someone else.

Take a look at the existing code:
100
101
102
103
104
105
106
107
108
109
110
    double highest;
    
    highest =rainfall[0];
    
    for (int count = 0; count < SIZE; count++)
    {
        if (rainfall[count] > highest)
        {
            highest = rainfall[count];
        }
    }

what needs to happen is, every time the value stored in highest changes, the subscript of the corresponding array element should be stored in our 'integer passed by reference' which I gave the name highIndex - though you can call it what you like, so long as it is meaningful.

The variable highest is modified in two places, at lines 102 and 108. Simply add a line of code immediately before or after each of those lines, to store the subscript.

I don't know how to be clearer without writing the code for you, and I'm reluctant to do that as people often copy & paste without understanding the code. I'm trying to help you both write the code and understand it (I hope).

Last edited on
Ok so the highIndex variable is what is confusing me. I understand that I am supposed to link it to the subscript of the months array but am having trouble understanding how exactly to do that.

When I try highIndex = months[0]; on line 103 I get an error that says "assigning to 'int' from incompatible type 'string'.

When I try highIndex = months[count]; I get the same error

I have tried highIndex = rainfall [count] but that just ends up returning a numeric value.
Last edited on
I don't understand what is the source of confusion. (other than of course, my poor explanations).

At line 103 the array months is not even visible, since it is defined in main() and we are discussing the code inside function getHighest()

So that obviously rules out highIndex = months[0]; completely. That the compiler gave the message, "assigning to 'int' from incompatible type 'string'" suggests you are really trying too hard, you must have done extra, unnecessary work to get that string visible in there.

I'll repeat what I stated in the previous post,
"what needs to happen is, every time the value stored in highest changes, the subscript of the corresponding array element should be stored in highIndex"

example:
 
    highest = rainfall[0];

what is the subscript here? it is simply 0.
so the required code is just:
 
    highIndex = 0;


Last edited on
I think the source of confusion is definitely on this end.....

Ok so If I try this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
double getHighest(double rainfall[], int SIZE, int& highIndex)
{
    int highest;
    
    
    highest =rainfall[0];
    highIndex = 0;
    
    for (int count = 0; count < SIZE; count++)
    {
        if (rainfall[count] > highest)
        {
            highest = rainfall[count];
            highIndex = rainfall[count];
        }
    }
    
    cout << highIndex;
     return highest;
}


The cout statement returns 0 instead something like "January .

The variable never changes with the rainfall[count] loop.

I have also tried it without the highIndex = 0 line but keeping in the highIndex = rainfall[count]; and that still assigns a 0 to highIndex.
1
2
highest = rainfall[count];
highIndex = rainfall[count];


I'm not sure you really want these to have the same value? The highest gets the actual value of rainfall (double), but wouldn't the highIndex be the index number (int) of the array that held that highest value? Essentially count, not rainfall[count]?

Thats what I'm confused about. I don't know how to assign highIndex

If I try
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
double getHighest(double rainfall[], int SIZE, int& highIndex)
{
    int highest;
    
    
    highest =rainfall[0];
    //highIndex = 0;
    
    for (int count = 0; count < SIZE; count++)
    {
        if (rainfall[count] > highest)
        {
            highest = rainfall[count];
            highIndex = count;
        }
    
    }
    
    cout << highIndex;
     return highest;
}
It still comes out to a value of 0 and if I try highIndex = [count] I get an error that says expected body of a lambda expression
The cout statement returns 0 instead something like "January

That cout statement should not be there. The printing of "January" should happen in main(), not here in this function. And yes, highIndex is indeed an integer. That's what we expect. It's defined as an integer so naturally if you output it, you will see a number. Though it should take any value in the range 0 to 11 since those are the possible values of the subscript for the array.


You do know what is meant by the term "subscript" don't you? It's the number which specifies which of the elements of the array we want to access. It is the number or expression which appears in the square brackets here: rainfall[0] or here: rainfall[count]

One way to approach programming (quite a good one actually) is even when you don't fully understand something, to look for patterns, look for symmetry.

Compare these two statements:
1
2
    highIndex = 0;
    highIndex = rainfall[count];
they look quite different. That's a big clue that something has gone wrong.

I'm repeating myself here.
The purpose of the parameter highIndex which is an integer, passed by reference, is to be able to return an integer value to the place where the function was called from.

After it has returned to main, you can do this (exact copy of what I posted yesterday).

With the help of the highIndex value, you can use this array of month names, still keeping within the main() function.
The code might look like this:
70
71
    cout << "The month of " << months[highIndex] 
         << " had the highest rainfall." << endl;


Note: this takes place in
main(), not in the function getHighest()



Thank you for taking the time to repeatedly answer questions that I can't seem to wrap my head around.

I only had the cout statement so that I could easily see what it was outputting. I know its not really supposed to be there. Is there a better way to see an output quickly?

So I got the program working. I doubt it's exactly the way it should be, but it works. The part where you explained cout << "The month of " << months[highIndex] << " had the highest rainfall." << endl; Really helped me out and made it click how the highIndex was supposed to work.

Here is what I did. comments?

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
double getHighest(double rainfall[], int SIZE, int& highIndex)
{
    int highest;
    
    
    highest =rainfall[0];
    
    
    for (int count = 0; count < SIZE; count++)
    {
        if (rainfall[count] > highest)
        {
            highest = rainfall[count];
            if (count >= 0)
            {
                highIndex = count;
            }
            
        }
        
    }
    
    
     return highIndex;
}
I don't have time to give a full reply right now (though i should be able to later).

It's good that you have at least partially understood what needs to be done.

There's still a problem. You've now lost the actual rainfall figure which was supposed to be returned as a value of type double. Instead you are in effect returning the same value via two different routes. Let me quote from one of my own previous posts, then I'll have to leave this for a while:

"The int & parameter I think you correctly understand is passed by reference. What that means is that the variable is defined in main() as an ordinary integer. The reason it's used is because in the ordinary way, a function can return only one value, and the function is already using that to return the actual rainfall value. So this integer passed by reference becomes a second value returned from the function, in effect."
Topic archived. No new replies allowed.