C++, Don't quite get the result I expect...

Started by
13 comments, last by BeatOne 18 years, 4 months ago
Hello there! Thought this would be the place for my small problem. When I convert from decimal to binary bases with a recursive function, it produces only the two last to digits of the result. If I input 54, it produces 00. If I input 55, it produces 01. Here is the code, would be happy if you could helo me. void toBinary(int dec) { int ans; if(dec <= 1){ cout << dec; return; } ans = dec % 2; toBinary(ans >> 1); cout << ans; }
Advertisement
toBinary(ans >> 1);
Should be:
toBinary(dec >> 1);
I assume it was a typo and you understand the problem, if you don't please ask why you should do that.
Your maths is flawed [smile]
Note that x MOD y returns the remainder of the division of x by y, so your code basically just has four outcomes:
0, 1, 00 and 01 [smile]
[edit]
Ah - beaten to it [smile]
[/edit]
Just a guess but I think it's here:

ans = dec % 2;
toBinary(ans >> 1);
cout << ans;

ans can only be 0 or 1 so when it's passed to toBinary "dec<=1" becomes true and it exits.

I think "toBinary(ans >> 1);" should be "toBinary(dec >> 1);".

Edit: aw crap you guys are too fast
Ahh.. Of course! Thank you guys.. :) Tired eyes always miss stuff like that out.
There's one other thing I am wondering about.. Can I append to an int?

Let me explain what I mean; I have a loop, spitting out numbers like 45, 67, 115, every run. I want a variable that when the loop is over, looks like this: 4567117

Understan me? :)
Quote:Original post by BeatOne
There's one other thing I am wondering about.. Can I append to an int?

Let me explain what I mean; I have a loop, spitting out numbers like 45, 67, 115, every run. I want a variable that when the loop is over, looks like this: 4567117

Understan me? :)


I assume you meant 4567115. If you wanted to store the result as an int you could just for each loop multiply the int with 10x. So if we start with having 45 we assign the "Final" int to 45, we then see 67 is the next number, 67 have two digits so we multiply with 100(two zeros), and then add 67 and then we multiply with 1000 (since 115 have 3 digits) and add 115. So the final calculation would be like this:

Final = (45 * 100 + 67)*1000 + 115

EDIT: A simple (and ugly) code example:
void Add(unsigned int x){	static unsigned int Final = 0;	if( 0 == Final )	{		Final = x;		return;	}	// Calculate number of digits of X by taking the log10, adding one and casting to unsigned int.	unsigned int NumDigits = static_cast<unsigned int>(std::log10(static_cast<float>(x))) + 1;	// Get the multiplier by: 10^NumDigits	unsigned int Multiplier = static_cast<unsigned int>(std::pow(10.0F,static_cast<int>(NumDigits) ) );	Final *= Multiplier;	Final += x;	std::cout << "Final: " << Final << std::endl;}
Thanks for that one! Will check that out later!
Or, you can use the Standard C++ Library to make life a lot easier [wink]

#include <sstream>using std::stringstream;...int var1 = 45;int var2 = 67;int var3 = 115;...// Create the streamstringstream ss;// Output all the variables heress << var1;ss << var2;ss << var3;// ss.str(), which returns a string, is now "4567115", but it's a string and you want an int// Store the intint result = 0;// extract the resultss >> result;// Now, result == 4567115, and you have your combined int, if you just wanted that string, use the ss.str() function.
personally i would just have a string variable which i kept appending the integer to inside the loop. would just be more obvious IMO.

EDIT: bah, drew_benton beat me to it.
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement