Feb 11, 2012 at 3:29pm UTC
At the call of decTobin(), it keeps saying there is a syntax error(error C2059: syntax error : ']') but I just don't see it. Am I not seeing something very obvious??
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
#include <iostream>
using namespace std;
void decTobin(int , int );
//void BinaryAnd(int*, const int*, const int*)
int main()
{
int num;
int n = 0;
int binNum[] = {0, 0, 0, 0, 0, 0, 0, 0};
cout << "Enter an integer" << endl;
cin >> num;
decTobin(num, binNum[]);
for (int n=0; n < 8; n++)
cout << n << ' ' << binNum[n] << endl;
return 0;
}
void decTobin(int num, int binNum[])
{
int n = 0;
int remainder;
if (num == 0)
{
return ;
}
else if (num == 1)
{
int n = 0;
while (n < 7)
{
binNum[n] = 0;
n++;
}
binNum[n] = 1;
}
else if (num > 0)
{
while (num >= 1)
{
remainder = num%2;
num = num/2;
binNum[n] = remainder;
n++;
}
}
else
{
num = abs(num);
while (num >= 1)
{
remainder = num%2;
num = num/2;
binNum[n] = remainder;
n++;
}
n = 0;
binNum[n] = 1;
n++;
while (n < 8)
{
if (binNum[n] = 0)
{
binNum[n] = 1;
n++;
}
else
{
binNum[n] = 0;
n++;
}
}
}
}
Last edited on Feb 11, 2012 at 3:37pm UTC
Feb 11, 2012 at 3:38pm UTC
The [] shouldn't be there. The name of the array is binNum. When not in a declaration, []s mark an operation. What kind of operation do you expect?
Feb 11, 2012 at 3:41pm UTC
I thought that at first but when I took away [], it said it couldn't convert from int [] to int.
Feb 11, 2012 at 3:49pm UTC
Change this :
void decTobin(int , int );
Into :
void decTobin(int , int []);
Also, change :
decTobin(num, binNum[]);
Into:
decTobin(num, binNum);
Last edited on Feb 11, 2012 at 3:50pm UTC
Feb 11, 2012 at 5:33pm UTC
Oh, I see. Silly mistake. Thank you.