Variables not storing properly

I am writing a function to passively load a map file into a vector and also display the percentage of completion.
For some reason the MapProgress variable which is defined as a static float will never store the outcome of line 12.
The only time it will get written to is when it finally reaches the else statement and then write 100.0 to it...but that happens when it finally finishes...am i doing something wrong...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void OpenMapFile()
{
	static int position = 0;
	int width = 1500;
	int height = 2000;
	int tile;
	
	if (position < width * height - 1)
	{
		tile = fgetwc(mapstore);
		p_mapdata.push_back(tile / 32 - 1);
		MapProgress = (position / (width * height)) * 100;
		
		position++;
	}
	else
	{
	MapProgress = 100.0;
	fclose(mapstore);
	}
}
(position / (width * height))

Since all of these variables are integers, the result will be an integer (0).

You'll need to convert one or more of them to floating point first.

1
2
3
// one possible solution:
MapProgress  = position * 100;
MapProgress /= width * height;


This works by involving a floating point (MapProgress) into the division, therefore you no longer get integer division.
Last edited on
Thank you!!! i did not know that was a requirement...now i know.
Topic archived. No new replies allowed.