Is this a truncation problem?

So I have this function, it needs to output a percent as an integer.

When numberWon and gamesPlayed are equal, it correctly gives me 100.
But for example if numberWon = 2 and gamesPlayed = 3, the percent ends up as 0.
Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
int PercentWon(int gamesPlayed, int numberWon)
{

	double percentCalc = (numberWon / gamesPlayed);

	int percentage = (static_cast<int>(percentCalc) * 100);

	cout << "You won " << percentage << "% of ";
	cout << "the games you played." << endl; 

	return percentage;
} 
It's not a truncation issue. numberWon and gamesPlayed are integers, so one divided by the other is integer division - so 2/3 or any other fraction less than 1 will return 0.
how would I fix that?

Use double instead of int. Or cast the whole result, not just percentage
Topic archived. No new replies allowed.