Output error in conversion

program not working as expected

Page 1 of 1

5 Replies - 407 Views - Last Post: 30 June 2010 - 07:08 AM Rate Topic: -----

#1 bloodzdevil  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 12-June 10

Output error in conversion

Posted 29 June 2010 - 08:35 AM

Hi guys.
I am trying to build a program that converts a Decimal number into its Binary counterpart. But I am facing a problem when I try to display the output.
It only displays junk. (0x8j..something)

Here's the code:

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<process.h>
#include <string.h>



void line(char ch)
	{
	int ctr=0;
	while(ctr<=50)
	{
	cout<<ch;
	ctr++;
	}
	cout<<endl;
	}
	
void header(char head[])
	{
	line('-');
	cout<<setw(25)<<head<<endl;
	cout<<setw(29)<<"by: Anshuman Mishra"<<endl;
	cout<<endl<<endl;
	line('*');
	cout<<endl<<endl;

	}



void Dec2Bin(int n)
	{
	const int max = 8;
	unsigned int bin[max];
	int j=0,rem,quot;
	while (n>0)
		{
	
		rem = n/2;
		bin[j] = n%2;
		n = rem;
	
		j++;
		}
	cout<<endl<<bin;
	}



int main()
	{
	for(int j=0;;j++)
		{
		clrscr();
		header("Numeral Convertor V1.01");
		line('*');
	

		int n ;
		cout<<"Please Enter the Number  ";
		cin>>n ;

		Dec2Bin(n);

	
		getch();
		exit(0);
		}
	return 0;
	}








Is This A Good Question/Topic? 0
  • +

Replies To: Output error in conversion

#2 GWatt  Icon User is offline

  • member icon

Reputation: 276
  • View blog
  • Posts: 3,075
  • Joined: 01-December 05

Re: Output error in conversion

Posted 29 June 2010 - 09:36 AM

In line 47 of the snippet, you print out the array. However, printing an array simply prints out the address of the array, not the contents on the array. You'll need to explicitly loop through and print out the contents.
Was This Post Helpful? 0
  • +
  • -

#3 bloodzdevil  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 12-June 10

Re: Output error in conversion

Posted 30 June 2010 - 04:33 AM

but even when i add the lines:

while (j>=0)
{
cout<<bin[j];
j--;
}



it prints out wrong result. For eg:

"52" has binary representation of 110100
but it prints : 52011010

This post has been edited by bloodzdevil: 30 June 2010 - 04:34 AM

Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Output error in conversion

Posted 30 June 2010 - 04:42 AM

while (j >= 0)
{
  cout<<bin[j];
  j--;
}


Well, take for example n = 52. It's binary representation is "110100". Therefore, j is 6, right? Because this is the size of your array. But now the question: is bin[j] a valid element in your array? It is not! Your "bin" array is indexed from 0 -> 6 - 1 (5). So, you have to output bin[j] after you decrease j, otherwise you will try to print bin[6] which isn't valid:
// Prevent writing bin[-1], that's why > 0 and not >= 0
while (j > 0)
{
   j--;
   cout << bin[j];
}


Hope that made sense...
Was This Post Helpful? 1
  • +
  • -

#5 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Output error in conversion

Posted 30 June 2010 - 04:43 AM

I don't know your logic for doing this, but I have done it with using the bitwise shift operators (<<, >>)

the rightmost bit makes the integer odd or even, it's pretty straightforward to (on little endian systems)

Loop:

if (int%2)
push_back('1');

int >>= 1;//shift the bits over 1, determine the state of the next bit.
Was This Post Helpful? 0
  • +
  • -

#6 bloodzdevil  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 12-June 10

Re: Output error in conversion

Posted 30 June 2010 - 07:08 AM

View Postsarmanu, on 30 June 2010 - 03:42 AM, said:

while (j >= 0)
{
  cout<<bin[j];
  j--;
}


Well, take for example n = 52. It's binary representation is "110100". Therefore, j is 6, right? Because this is the size of your array. But now the question: is bin[j] a valid element in your array? It is not! Your "bin" array is indexed from 0 -> 6 - 1 (5). So, you have to output bin[j] after you decrease j, otherwise you will try to print bin[6] which isn't valid:
// Prevent writing bin[-1], that's why > 0 and not >= 0
while (j > 0)
{
   j--;
   cout << bin[j];
}


Hope that made sense...



Perfect!! It worked!! Thanks.....Hope it sticks to mind ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1