Invlaid conversion errors

My code keeps giving me an invalid conversion error when I try to pass the array elements for January into my avg_daily function. Is there a error in my declaration?

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
  #include<iostream>
#include<math.h>
#include<conio.h>

float getMonthly(const int);
float avg_daily( int days[], float month[]);
using namespace std;

int main(){

int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
int Jan_Rain=0;
int Feb_Rain=0;
int Year_Rain=0;
float month[1];
int month_number=0;
float avg_daily_rain=0;
float Jan_avg=0;


    Jan_Rain = getMonthly(days[0]);
        cout<<"The monthly rainfall for January is ";
        cout<<Jan_Rain<<endl;
    month[0]=Jan_Rain;

    Feb_Rain = getMonthly(days[1]);
        cout<<"The monthly rainfall for February is ";
        cout<<Feb_Rain<<endl;
    month[1]=Feb_Rain;

        cout<<"The total rainfall for the year is ";
        cout<<Year_Rain<<endl;
for
    (month_number=0; month_number<=1; month_number++)
	{
    Year_Rain+=month[month_number];
	}
        cout<<Year_Rain<< endl;

	Jan_avg= avg_daily(days[0],month[0]);
        cout<<"The average daily rainfall for january is";
        cout<<Jan_avg<<endl;

return 0;





getch();



}



float getMonthly( const int days )

{
  float rain_total = 0.0f;
  int day = 0;
  for ( int day = 0; day < days; ++day )
    {
        cout << "\nPlease enter the rainfall for day " << 1+day << ": ";
      float rainfall = 0.0f;
        cin >> rainfall;
      rain_total += rainfall;
    }
  return rain_total;
}


float avg_daily(  int days[], float month[])
{
    float avg=0.0f;
    int number=0;
    for(int number=0; number<=11; ++number){
     avg= month[number]/days[number];
    }


        cout<<" The average is";
        cout<<avg<<endl;
}
those errors you're reporting are actually warnings. for example:

Jan_Rain = getMonthly(days[0]);
your method is returning a float but you're trying to force that result into an integer.


however, you do have some errors in there as well:
etc:

'avg_daily' : cannot convert parameter 1 from 'int' to 'int []'


your method is expecting the whole array to be passed in but you're only passing in the first element of that array.

and missing braces and/or semi-colons.
Last edited on
they come up as errors and when try to pass the whole array it says that there's an expected primary token before ']' token
lines 12, 13 and 14, change these variable types to double rather than int.
That *should* get rid of your conversion warnings.
Change line 40 to

Jan_avg= avg_daily(&days[0],&month[0]);

or

Jan_avg= avg_daily(days,month);


on line 29: you access the second element while the definition has only one on line 15
If i set the array value to 1 doesn't it have 2 elements stored as {0,1}
and it looks like [code] Jan_avg = avg_daily(&days[0],&month[0]) works the error is gone
the average keeps coming up as zero
Function avg_daily() doesn't need a loop. But it does need to know the number of the month you are dealing with.
example
 
    Jan_avg = avg_daily(days, month, 0); // 0 since January is the first month 

1
2
3
4
double avg_daily(int days[], double month[], int number)
{
    return month[number] / days[number];
}

(I used double rather than float throughout).
You're right it doesn't but this code didn't work until I modified it to
1
2
3
4
5
6
7
8
float avg_daily(int days[], float month[], int number){

    return month[number];
    return days[number];
    
    float avg=0;
    
    avg=month[number]/days[number];}

but it still just produces the value of the number of days
Last edited on
I'm afraid i don't understand. If the correct code is:
 
 return month[number] / days[number];
why would you put:
 
 return month[number];
because I need the result to be stored in the variable average so I can call for it
sorry apparently I'm getting too attached to my variables your code works exactly as it should
I have another error where a 0 appears just before the actual value of yearly rainfall any suggestions?
31
32
33
34
35
36
37
38
        cout<<"The total rainfall for the year is ";
        cout<<Year_Rain<<endl;
for
    (month_number=0; month_number<=1; month_number++)
	{
    Year_Rain+=month[month_number];
	}
        cout<<Year_Rain<< endl;

Possibly you already have this solved. Just pointing out the line 32 is not needed.
Topic archived. No new replies allowed.