0

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]);}
3
Contributors
2
Replies
14
Views
3 Years
Discussion Span
Last Post by tinstaafl
0
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;

Edited by Moschops

0

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.

Edited by tinstaafl

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.