ISO C++ forbids comparison between pointer and integer

I need to get the lowest value of rainfall for each month
but there seems to be a problem with my function definition.
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
119
120
121
122
 #include<iostream>
#include<math.h>
#include<conio.h>



float getMonthly(const int);
float avg_daily( int days[], float month[], int number);
float lowest_day(float rainfall[],int day[], int days[]);

using namespace std;

int main(){

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


    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;




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],0);
        cout<<"The average daily rainfall for january is ";
        cout<<Jan_avg<<endl;

Jan_Low= lowest_day(rainfall, day, days[0]);
        cout<<" The lowest rainfall for January is";
        cout<<Jan_Low<<endl;

getch();
}



float getMonthly( const int days )

{
  int smallest,largest;
  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[30] ;
        cin >> rainfall[day];
      rain_total += rainfall[day];





    }

  return rain_total;
}





    float avg_daily(int days[], float month[], int number){

        return month[number]/days[number];

    }



float lowest_day(float rainfall[], int day[], int days[])
{


int smallest=0;
int position=0;
      smallest =rainfall[0];

    for (int day =0; day<days ; ++day)
    {

       if(rainfall[day]<smallest){
        smallest=rainfall[day];
        position=day;


       }

    }
    return smallest;

       }
line 108, you have a day[] variable as parameter, yet you have another one in
the for loop, as an index, change that index name to something else

and as your title says, you are attemping to compare
an integer-> day
to a pointer -> days which is eveluated to &days[ 0 ]
1
2
3
4
for( int i = 0; i < sizeof( days ) / sizeof( int ); i++ )
{
    ...
}


Jan_Low= lowest_day(rainfall, day, days[0]);

you have no day variable named day declared in that scope( or in main )
Last edited on
Made those corrections and a bunch of new errors including the original error popped up
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
119
120
121
#include<iostream>
#include<math.h>
#include<conio.h>



float getMonthly(const int);
float avg_daily( int days[], float month[], int number);
float lowest_day(float rainfall[],int day[], int days[]);

using namespace std;

int main(){

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

    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;




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],0);
        cout<<"The average daily rainfall for january is ";
        cout<<Jan_avg<<endl;

Jan_Low= lowest_day(rainfall, day, &days[0]);
        cout<<" The lowest rainfall for January is";
        cout<<Jan_Low<<endl;

getch();
}



float getMonthly( const int days )

{
  int smallest,largest;
  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[30] ;
        cin >> rainfall[day];
      rain_total += rainfall[day];





    }

  return rain_total;
}





    float avg_daily(int days[], float month[], int number){

        return month[number]/days[number];

    }



float lowest_day(float rainfall[], int day[], int days[])
{


int smallest=0;
int position=0;
      smallest =rainfall[0];

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

       if(rainfall[day]<smallest){
        smallest=rainfall[i];
        position=i;


       }

    }
    return smallest;

       }
. I was considering one solution was to input the if statement in the getMonthly function but I'm not sure how to place it or call the value of smallest and the relevant position of the date.
float lowest_day(float rainfall[],int day[], int days[]);
should be :
float lowest_day( float rainfall[], int day, int days[] );

change the function implementation as well in line 100

for (int i =0; i<days ; ++i)
should be
for( int i = 0; i < sizeof(days) / sizeof(int); i++ )
Last edited on
Ok done now there's an int to int* conversion error and an error for initializing argument 2 in line 57.
change that to Jan_Low= lowest_day(rainfall, day, days);

and also change line 53 to :

Jan_avg= avg_daily( days, month, 0);
Last edited on
same problem and why should I change line 53 it actually works fine?
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
#include<iostream>
#include<math.h>
#include<conio.h>



float getMonthly(const int);
float avg_daily( int days[], float month[], int number);
float lowest_day(float rainfall[],int day, int days[]);

using namespace std;

int main(){

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

    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;

	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],0);
    cout<<"The average daily rainfall for january is ";
    cout<<Jan_avg<<endl;

	Jan_Low= lowest_day(rainfall, day, days);
    cout<<" The lowest rainfall for January is";
    cout<<Jan_Low<<endl;

	cin.get();
}

float getMonthly( const int days )
{
  int smallest,largest;
  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[30] ;
        cin >> rainfall[day];
		rain_total += rainfall[day];
    }

  return rain_total;
}

float avg_daily(int days[], float month[], int number)
{
    return month[number]/days[number];
}

float lowest_day(float rainfall[], int day, int days[])
{
	int smallest=0;
	int position=0;
    smallest =rainfall[0];

    for( int i = 0; i < sizeof(days)/sizeof(int); ++i )
    {
       if( rainfall[ i ] < smallest ){
        smallest=rainfall[ i ];
        position=i;
       }
    }
    return smallest;

}
what's the purpose of the division in the for loop for the lowest day function?
The function works to produce output but gives an extremely erroneous value. I don't get it. Everything is initialized.
I looked at this the other day, in fact debugged the whole thing, but deleted my code, as that thread was marked as solved.

Anyway, in the code from the opening post of this thread, at line 19:
 
float month[1];
That's an array with one element. It can hold the data for January - at line 30, this is ok:
 
month[0]=Jan_Rain;
But that's it. Full up now. So what happens at line 35?
 
month[1]=Feb_Rain;

Well, the value is stored in an adjacent memory location, outside the array, and some other data gets corrupted. And that is a serious problem.

So make that array at least
 
float month[2];
or maybe
float month[12]; to hold an entire year's worth of data.

In function getMonthly() starting at line 66, there is a for loop.
inside the loop a new array float rainfall[30] ; is declared. Now its a bit pointless declaring it there, as only one element is ever used before it gets destroyed and a fresh version created in the next iteration of the loop. But it doesn't do any harm, does it?

Well, actually it does a lot of harm. Note it is declared as having 30 elements. That's good enough for some months. But what happens in January, and get to line 76 cin >> rainfall[day]; on the 31st day. Where does the input value go? Well, you can be pretty sure it's somewhere bad. Some other value will get corrupted, with unpredictable effects.

If you really need to store each individual value, then you need a different solution. But for now I'd say just simplify the function like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
float getMonthly( const int days )
{
    float rain_total = 0.0f;
    float rainfall;

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


Those are the big problems.

There's still the minor one of line 38 cout<<Year_Rain; which is not needed - as I pointed out previously. http://www.cplusplus.com/forum/beginner/118142/#msg644350
Last edited on
Topic archived. No new replies allowed.