Easy Question-C++ Dissecting an int for char use

Started by
4 comments, last by ubersheep 17 years, 4 months ago
Hi - I'm writing a C++ console-based game, as I am bored. I need/want to update the screen each frame with the score. I have an int value: playerScore, and a DrawChar() function - DrawChar(short x, short y, const char c, short colour) I'd really like, if possible, to be able to NOT use strings (or string.h, or anything that uses string.h) I've spent an hour trying to figure out why this won't display properly, and I've narrowed it down to the conclusion that integer division rounds properly to the nearest number, but to be honest I really don't know if that's just the problem.

#include <iostream>
#include <Math.h>

int main()
{
... << lots of code here etc

for (int i = 1; i < 8; ++i)
{
	if ( playerScore >= pow((double)10,7-i) )
	{
		//Retrieves the next value (reading from left),
		//by dividing by 10
		newChar = (int)(playerScore / pow((double)10,7-i));

		//Removes the excess values
		int temp = (int)pow(10.0f,8-i);
		newChar %= temp;

		if (newChar == 10)
		{
			newChar = 0;
		}
		DrawChar(i,0,(char)(newChar + 48),WHITE);
	}
}




The above code compiles well, but when executed, anything beyond the most significant figure (the number of the far left) becomes scrambled. I.e. 100, (after each point increase of 100) 2D0, 3N0, 4X0, ... 9T0, 1=N0, 1>X0, 1?B0, etc Can anyone shed some light on this? Apologies if I'm in the wrong forum, I couldn't decide between here, the beginners forum, or the math forum. Thanks in advance :) Edit: Example output posted
Advertisement
Don't mix floating point values in when doing this sort of thing. You should be able to do this entirely with integer math. It may be easier to extract the digits starting with the least significant place. By avoiding floating point values you can be sure that numerical rounding issues won't affect your results.

As it is, even assuming floating point numbers have unlimited precision your code has a logic error in it.

Let's say playerscore is 1234, and we're trying to extract the tens place. I believe this corresponds to i=6.
newChar = 1234/10 = 123
temp = 100
newChar = 123%100 = 23

I think your code only works correctly for the most significant digit and the ones place. temp should always be equal to 10, instead of picking a different value based on place. The division already placed the digit you are extracting into the ones place, no matter which place you are extracting.
Yes!!! Thankyou! I don't know what I was thinking last night, but that totally solved it. Thank you so much :D

//Example code#include <iostream>#include <Math.h>void DrawChar(short x, short y, const char c, short colour){	std::cout << c << '\n';	return;}int main(){	int playerScore = 103042;	int nextDigit;	const int WHITE = 00;	// playerScore is 00103042	// display all available digits	for (int i = 1; i < 8; ++i)	{		if ( playerScore >= pow((double)10,7-i) )		{			//Retrieves the next value (reading from left),			//by dividing by 10			nextDigit = (int)(playerScore / pow((double)10,7-i));			//Removes the excess values			nextDigit %= 10;			DrawChar(i,0,(char)(nextDigit + 48),WHITE);		}	}	return 0;}


You were right, 'temp' should have been 10 each time, to get the least sig figure of each division. :)

Edit: Spelling, [ source ] didn't work, works now

Edit2: Interestingly enough, if you change nextDigit to become a char instead of an int, you get strange results...? (Because char doesn't have anywhere near the precision int has perhaps?)
I'm a little confused by your padding logic. If the number is smaller than 7 digits then you pad the left with empty space. And if it's more than 7 digits you just start printing it out. However both cases take place within a loop that runs exactly 7 times, so if the number was longer than 7 digits you'd only print the 7 most significant digits?

You could simplify your code by printing the number from right-to-left instead of left-to-right (using a clever modulus/division combo) and pre-calculating the starting value of i using a single logarithm. That way you can avoid extracting arbitrary digits using ugly exponent math.
It's a simple Snake game, so with it, I'm not anticipating the score climbing above 999,999 , let alone 9.9 mil. .
I'm going to try to rewrite it to print the number R2L, that does make a lot more sense.

Note to self, NEVER code while tired, spaghetti happens.
Vorpy & Zipster, thankyou both, my spaghetti is now 10 tidy lines of code:

int newChar = 0;int pos = 1;int iter = 1;while ( playerScore > iter-1 ){	newChar = (int)(playerScore / iter) % 10;	DrawChar(( 8 - pos ), 0,(newChar + 48),WHITE);	pos++;	iter *= 10;}

This topic is closed to new replies.

Advertisement