Passing arrays as an argument to a function

I thought I was done with this code, but when I try to compile it it says invalid conversion from 'int' to 'int*'[-fpermisive]. I know what's wrong but I don't have a clue how to fix it without redoing my entire code and seriously over complicating it.
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 <string>

using namespace std;

//AND Gate
int fand(int A[10], int B[10])
{
    int C[10];
    for(int i=0;i<10;i++)
    {if((A[i]==1)&&(B[i]==1))
        C[i]=1;
    else
        C[i]=0;}
        return C[10];
}
//OR Gate
int fuor(int A[10], int B[10])
{
    int C[10];
    for(int i=0; i<10;i++)
    {if((A[i]==1) || (B[i]==1))
    C[i]=1;
    else
        C[i]=0;}
    return C[10];
}
//XOR Gate
int fxor(int A[10], int B[10])
{
    int C[10];
    for(int i=0;i<10;i++)
    {;
    if((A[i]==1) && (B[i]==0))
        C[i]=1;
    else if((A[i]==0)&&(B[i]==1))
        C[i]=1;
    else
        C[i]=0;}
        return C[10];
}
//NAND Gate
int fnand(int A[10], int B[10])
{
    int C[10];
    for(int i=0; i<10; i++)
    {
        if((A[i]==1)&&(B[i]==1))
        C[i]=0;
    else
        C[i]=1;}
        return C[10];
}
//NOR Gate
int fnor(int A[10], int B[10])
{
    int C[10];
    for(int i=0;i<10;i++)
    {
        if((A[i]==0) && (B[i]==0))
        C[i]=1;
    else
        C[i]=0;
    }
    return C[10];
}
//XNOR GATE
int fxnor(int A[10], int B[10])
{
    int C[10];
    for(int i=0;i<10;i++)
    {
        if((A[i]==0)&&(B[i]==0))
        C[i]=1;
    else if((A[i]==1)&&(B[i]==1))
       C[i]=1;
    else
        C[i]=0;}
        return C[10];
}


int main()
{
   int A[10];
   int B[10];
   char op;
   cout<<"Input your first Boolean value either T/F=";
   cin>>A[10];
   cout<<"Input your operator A(and), B(or), C(xor), D(nand), E(nor), F(xnor=)";
   cin>>op;
   cout<<"Input your second Boolean value either T/F=";
   cin>>B[10];
      switch (op)
   {
    case 'A':
        fand(A[10],B[10]);
        cout<<fand(A[10],B[10]);
        break;
    case'B':
        fuor(A[10],B[10]);
        cout<<fuor(A[10],B[10]);
        break;
   case 'C':
      fxor(A[10],B[10]);
      cout<<fxor(A[10],B[10]);
        break;
    case 'D':
        fnand(A[10],B[10]);
        cout<<fnand(A[10],B[10]);
        break;
    case 'E':
        fnor(A[10],B[10]);
        cout<<fnor(A[10],B[10]);
        break;
    case 'F':
        fxnor(A[10],B[10]);
        cout<<fxnor(A[10],B[10]);
        break;
   }
   return 0;
}
Instead of
1
2
3
4
    case 'A':
        fand(A[10],B[10]);
        cout<<fand(A[10],B[10]);
        break;

try
1
2
3
4
    case 'A':
        fand(A,B);
        cout<<fand(A,B);
        break;

A[x] means the item at index 'x', which is why the compiler is telling you that you're passing an int to a function that expects a pointer to int.
That seems to work, but whatever I do in case A it always seems to answer 10, should be able to work it out on my own, thanks for the help :)
You should go back and read more about arrays, because you are making the same mistake on input.
1
2
   cout<<"Input your first Boolean value either T/F=";
   cin>>A[10];

You're addressing one element in A in particular, and in this case, that element is out of bounds of your array. If you declare an array of 10 items int A[10]; then you refer to them as A[0], A[1], A[2],..., A[8], A[9]. There is no A[10]. If you write to A[10] you're writing outside the bounds of your array and you might corrupt your program state and (hopefully) segfault.

Another problem is how you're returning values from your functions. Again, you are returning a single value when you probably intend to return an entire array.

In the end, why are you using arrays at all? You're only reading one boolean value at a time, not a collection of them.
Last edited on
Topic archived. No new replies allowed.