odd problems with % operator result when turning it into a unsigned char variable

Started by
5 comments, last by Zahlman 17 years, 10 months ago
This may not be the right forums for this question but it kind fits in a few places. This comes from a current project of mine which is a random height map generator. I recently modifyed it to work with heights beyond 255 so it is useing doubles to do this. I made a mini map that showed a greyscale image of it so i tryed to get it to show in greyscale then other color types to make it slightly more useful. First off is the code. It is cut a bit to show the code dealing with my problems (I mean there is more there but its not relevent). What is not shown is that buffer[] is a double pointer to a array of data, data[] is a pointer to a array of unsigned char, RemanderOutput is a file output object from ofstream, and Width and Height are int's defined else where


	int x, z, Remander, temp;

	x = 0;
	z = 0;

	for(x = 0; x < (Width * Height); x++) {

		temp = buffer[x];

		Remander = temp%255;

		if(buffer[x] <= 255) {

			Data[z] = buffer[x];
			z++;
			Data[z] = buffer[x];
			z++;
			Data[z] = buffer[x];
			z++;
		}//end buffer[x] <= 255 if

		//red if's
		if(buffer[x] > 255) {
			if(buffer[x] <= 510) {
				if(Remander == 0) {
					Data[z] = 255;
					z++;
					Data[z] = 0;
					z++;
					Data[z] = 0;
					z++;
				}//end Remander if

				else {

					RemanderOutput << "Remander: " << Remander << std::endl;
					Data[z] = Remander;
					RemanderOutput << "Data: " << Data[z] << std::endl;
					z++;
					Data[z] = 0;
					z++;
					Data[z] = 0;
					z++;
				}//end else
			}//buffer[x] <= 510
		}//buffer[x] > 255


When i run my program and make the heightmap i output the data for the else to a text file. Remander seems to have the right value. However when i assign it to Data[z] it gives me a value of . I am not real use to using the % operator. only done it a few times and never ran into this befor and any information on why this is happening would be apreciated
Advertisement
It doesn't look like it has anything to do with the % operator. Rather, it has everything to do with the fact that you're outputting a char, which outputs an ASCII* character rather than the textual representation of an 8-bit number. If you want that, cast it to int. For instance:
char c = 65;cout << c << endl; // Outputs "A"cout << (int)c << endl; // outputs "65"

BTW: Your problems with localizing this bug are directly caused by the fact that you're using output statements to do your debugging. If you learn how to use the debugger, these issues will be trivial to pinpoint and fix.



*Or EBCDIC, if you're kicking it old school.
Quote:*Or EBCDIC, if you're kicking it old school.


Does anybody still kick it that old school?

Check out my new game Smash and Dash at:

http://www.smashanddashgame.com/


You where right and i want to ask if you know of any tutorials on how to use Visual Studio Net's debugger.
Hottentot
FYI, x%y will be a number less than y. That is to say, I think:

Remander = temp%255

should be either:

Remander = temp%256; or Remander = temp&255;
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Also FYI, it's spelled "remainder", with an i. :)

This topic is closed to new replies.

Advertisement