"expected primary expression before const" and "invalid types for array subscript" errors

I have to make 2 arrays and use functions ti input, output, add, and compare them but I keep getting "expected primary expression before const" and "invalid types for array subscript" errors

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
#include <iostream>
using namespace std;

const int maxDigits = 100;
void inputVLI (int vli[], int& size);   // Inputs the very long integers
void outputVLI (const int vli[], int size); // Outputs the very long integers
void addVLI (const int vli1[], const int vli2[], int vli3[], int size1, int size2, int& size3); // Adds the very long integers together
int compVLI (const int vli1, const int vli2, int size1, int size2); // Compares the very long integers

int main()
{
    int vli1[maxDigits], vli2[maxDigits], vli3[maxDigits], size1, size2, size3;
    inputVLI (vli1, const maxDigits);
    outputVLI (vli1, const maxDigits);
    inputVLI (vli2, const maxDigits);
    outputVLI (vli2, const maxDigits);
    addVLI (vli1, vli2, vli3, size1, size2, size3);
    compVLI (vli1, vli2, size1, size2);
    return 0;
}

void inputVLI(int vli[], int& size)
{
    cout << "Enter a very long integer" << endl;
    cin >> vli[maxDigits];
}

void output (const int vli[], int size)
{
    for (int i=maxDigits; i>=0; i--)
        cout << vli[i];
}

void addVLI (const int vli1[], const int vli2[], int vli3[], int size1, int size2, int& size3)
{
    for (int i=0; i<maxDigits; i++)
    {
        vli3[i]=vli1[i] + vli2[i];
        cout << "The sum of the vli's is:" << vli3[i] << endl;
    }
}

int compVLI (const int vli1, const int vli2, int size1, int size2)
{
    for (int i=0; vli1[maxDigits] && vli2[maxDigits]; i++)
    {
        if (vli1[maxDigits] != vli2[maxDigits])
        {
            return (-1); // -1 is not equal
        }
        return (1); // 1 is equal
    }
}
The first thing I see is that you don't need the const keyword in your function calls.

This program has quite a few mistakes, it looks like you waited too long before you compiled the code. It might be easier to start with something like:

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
#include <iostream>
using namespace std;

const int maxDigits = 100;
void inputVLI (int vli[], int& size);   // Inputs the very long integers
void outputVLI (const int vli[], int size); // Outputs the very long integers
void addVLI (const int vli1[], const int vli2[], int vli3[], int size1, int size2, int& size3); // Adds the very long integers together
int compVLI (const int vli1, const int vli2, int size1, int size2); // Compares the very long integers

int main()
{
    int vli1[maxDigits], vli2[maxDigits], vli3[maxDigits], size1, size2, size3;

    inputVLI (vli1, const maxDigits);
/*    outputVLI (vli1, const maxDigits);
    inputVLI (vli2, const maxDigits);
    outputVLI (vli2, const maxDigits);
    addVLI (vli1, vli2, vli3, size1, size2, size3);
    compVLI (vli1, vli2, size1, size2);
*/
    return 0;
}

void inputVLI(int vli[], int& size)
{
/*
    cout << "Enter a very long integer" << endl;
    cin >> vli[maxDigits];
*/
}

void output (const int vli[], int size)
{
/*
    for (int i=maxDigits; i>=0; i--)
        cout << vli[i];
*/
}

void addVLI (const int vli1[], const int vli2[], int vli3[], int size1, int size2, int& size3)
{
/*
    for (int i=0; i<maxDigits; i++)
    {
        vli3[i]=vli1[i] + vli2[i];
        cout << "The sum of the vli's is:" << vli3[i] << endl;
    }
*/
}

int compVLI (const int vli1, const int vli2, int size1, int size2)
{
/*
    for (int i=0; vli1[maxDigits] && vli2[maxDigits]; i++)
    {
        if (vli1[maxDigits] != vli2[maxDigits])
        {
            return (-1); // -1 is not equal
        }
        return (1); // 1 is equal
    }
*/
}


Get this to compile without errors, then uncomment the next line in main(), etc until you're finished with main. Then move on to the functions, uncomment out the code in one function at a time and fix the problems, then move on to the next function until your finished.

When I take the "const" out of the function call i get: error: invalid initialization of reference of type 'int&' from expression of type 'const int'

I've never seen this before, I don't know what it means
It means that since maxDigits is a constant you can't pass the variable to the function using a reference parameter. Just pass this variable by value instead.

Topic archived. No new replies allowed.