4 Replies - 155 Views - Last Post: 05 February 2012 - 12:10 PM Rate Topic: -----

Topic Sponsor:

#1 Stellar92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-February 12

Cannot figure out why it is printing incorrectly

Posted 05 February 2012 - 11:46 AM

I am having trouble with this code. I am trying to convert decimal to binary using string functions and when it runs I get weird symbols instead of the binary digits. Help please?

#include <iostream>
#include <string>
using namespace std;

string reverse (string s)
{
	string result(s);
	int i = 0;

	for (int i = 0, j = s.length() - 1; i <s.length(); i++, j--)
		result[i] = s[j];
	return result;
}

string convertDecimalToBinary(int value)
{
	string s;
	int x = 1;
	int y = 0;

	while (value != 0)
	{
		int number = value % 2;
		if (number == 1)
			s.append(1, x);
		if (number == 0)
			s.append(1, y);
		value = value / 2;
	}
	return reverse(s);
}

int main()
{
	cout << "Enter a decimal number: ";
	int number;
	cin >> number;
	cout << convertDecimalToBinary(number) << endl;

	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Cannot figure out why it is printing incorrectly

#2 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 54
  • View blog
  • Posts: 136
  • Joined: 04-August 08

Re: Cannot figure out why it is printing incorrectly

Posted 05 February 2012 - 11:56 AM

Just looking at this quickly it may be because you're appending the integer values 0 or 1, which in ASCII corresponds to something else. What you want is the ASCII representation of 0 or 1, which would be 48 and 49. This can be fixed by maybe this
if (number == 1)
   s.append(1, '1');
if (number == 0)
   s.append(1, '0');


Hope I've helped :)
Was This Post Helpful? 2
  • +
  • -

#3 ishkabible  Icon User is online

  • spelling expert
  • member icon



Reputation: 1142
  • View blog
  • Posts: 4,783
  • Joined: 03-August 09

Re: Cannot figure out why it is printing incorrectly

Posted 05 February 2012 - 11:59 AM

s.append(1, x);


that dosn't do what you think it does; that appends the assci character with value '0' which is a control character not the decimal '0'; instead just use s.push_back('0') and s.push_back('1')
Was This Post Helpful? 2
  • +
  • -

#4 Stellar92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-February 12

Re: Cannot figure out why it is printing incorrectly

Posted 05 February 2012 - 12:09 PM

View Postishkabible, on 05 February 2012 - 11:59 AM, said:

s.append(1, x);


that dosn't do what you think it does; that appends the assci character with value '0' which is a control character not the decimal '0'; instead just use s.push_back('0') and s.push_back('1')



thank you very much! :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,898
  • Joined: 02-June 10

Re: Cannot figure out why it is printing incorrectly

Posted 05 February 2012 - 12:10 PM

I took your C++ and made minor syntactical changes to make it C# so I could follow it and produce some breakpoint results.

I fed the function the number 47

The first function produced 111101
The second produced 101111

Attached Image

Attached Image

So to me its seems you logic is sound. So it has to be some of your statements don't work the way you assume they do. I'm going to guess the string.append might be worth a read.

Update... Looks like some others type faster than I do.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1