how come compiler is displaying this problem even though I've declared the array.

#include <iostream>
#include <conio.h>
using namespace std;
void toBinary(long long decimal, int binary_number_array[], int & NumBinaryBits);

int main()
{

long long decimalNumberToConvert[1] ;
cout<<"Enter a decimal number"<<endl;
cin>>decimalNumberToCovert[1];
int sizeBinaryNumber, binaryNumberArray[100];
toBinary(decimalNumberToConvert, binaryNumberArray, sizeBinaryNumber);

//Printing out the binary number
cout << " The binary equivalent of " << decimalNumberToConvert << " : ";
for (int i = 0; i < sizeBinaryNumber; ++i)  
cout << binaryNumberArray[i];
cout <<endl;

getch();
return 0;
}
void toBinary(long long decimal, int binary_number_array[], int & NumBinaryBits)
{
   long long decimalNumberToConvert[1] = decimal;
   NumBinaryBits = 0;
do
{
binary_number_array[NumBinaryBits++] = decimalToConvert % 2;
decimalNumberToConvert[1] /= 2;
}
while (decimalToConvert);
for (int i = 0; i <= NumBinaryBits / 2; ++i) std::swap(binary_number_array[i], binary_number_array[NumBinaryBits - i - 1]);}

Recommended Answers

All 2 Replies

decimalNumberToCovert[1];

Spelling error. You missed an 'n'.

Also, when you make an array of size one, which is what you did, the elements start at element ZERO. So this:long long decimalNumberToConvert[1] ;
creates an array of a single long long, which you can get access to like this:decimalNumberToConvert[0] ;
This: decimalNumberToConvert[1] ; does not exist.

Also, there is no point in making an array of size one. Why not just make a single object? So instead of this:long long decimalNumberToConvert[1] ;
just do this: long long decimalNumberToConvert;

how come compiler is displaying this problem even though I've declared the array.

Mainly because you didn't spell it right. You missed the 'n'.

Also in toBinary, decimalToConvert is undefined.

This is one aread that VC++ really shines. Intellisense would have flagged that as you were typing it, making it much easier to spot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.