Cannot convert int to double

Apr 15, 2020 at 2:38pm
Hello, a beginner currently learning highest values.

Been trying to find the highest amount of mushrooms collected in a task, but keep getting this error:

error: cannot convert 'int*' to 'double*' for argument '1' to 'int HighestAmount(double*, int)'|

I have no idea what this means and I have no idea how to fix it. Would appreciate any help.

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>

const char CDfv[] = "data.txt";
const char CRfv[] = "results.txt";
const int CMax = 24;

using namespace std;
void Read(const char fv[], int A[], int B[], double C[], int & n);
int HighestAmount(double C[], int n);

void Read(const char fv[], int A[], int B[], double C[], int & n)
{
         ifstream fd(fv);
         fd >> n;
         for (int i = 0; i < n; i++)
            fd >> A[i] >> B[i] >> C[i];
         fd.close();
}

int HighestAmount(double C[], int n)
{
    double max = C[0];
    int maxind = 0;
    for (int i = 1; i < n; i++)
      if (C[i] > max) {
        max = C[i];
        maxind = i;
      }
    return maxind;
}

int main()
{
    int M[CMax];                            //month
    int D[CMax];                            //day
    double K[CMax];                         //mushroom amount
    int n;                                  //foraging amount
    double indmax;
    ofstream fr;
    Read(CDfv, M, D, K, n);
    fr.open(CRfv, ios::app);
    indmax = HighestAmount(M, D, K, n);
    fr << "HighestAmount: " << M[indmax] << D[indmax] << K[indmax] << endl;
    fr.close();
    return 0;
}
Last edited on Apr 15, 2020 at 2:42pm
Apr 15, 2020 at 2:55pm
The problem is that HighestAmount asks for a double array as its first parameter. However, in line 45, you pass it M, which is an int array.

Also, you seem to be passing HighestAmmount 4 arguments, when it only expects two: a double[] and an int.

Also, I suggest looking into std::string and std::string_view in the future for string representation. They are a lot more flexible than regular character arrays and have a lot more functionality.
Last edited on Apr 15, 2020 at 3:13pm
Apr 15, 2020 at 3:12pm
So it should be:

 
indmax = HighestAmount(K, n);


That fixed that part! Although now it gives these errors:

1
2
3
error: invalid types 'int [24][double]' for array subscript
error: invalid types 'int [24][double]' for array subscript
error: invalid types 'double [24][double]' for array subscript
Apr 15, 2020 at 3:15pm
You are subscripting the arrays M, D, and K with indmax, which is a double variable. This is not allowed. Subscripts can only be integers. You should change indmax to an int.
Last edited on Apr 15, 2020 at 3:19pm
Apr 15, 2020 at 3:24pm
1
2
3
4
5
int HighestAmount(double C[], int n);

    double indmax;
    indmax = HighestAmount(K, n);
    fr << "HighestAmount: " << M[indmax];

Your function returns int value, but you store it in double variable. Then you try to use that variable as index. Index must be an integer.
Apr 15, 2020 at 3:27pm
Thank you so much!! Both of you are lifesavers!
Topic archived. No new replies allowed.