Need Help With Batting Average in c++

I keep getting the wrong result when i run the program i get 44.5596 but what i am supposed to get is 0.446 my mistake is probably small but i just don't get it.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

const int AT_BAT = 579;
const int HITS = 258;

int main()

	float batAvg;

	batAvg = HITS * 100.00 / AT_BAT; 			    	  
	cout << "The batting average is " << batAvg << endl;   
	return 0;
From what I have read the usual way to calculate batting average is just HITS / AT_BAT. If you want 0.446 just remove the *100 from your code?
no i get 0 if i do that
Well, you could divide by 100.0 at the end of the expression, then. :/

EDIT: Oh, apologies, I completely misinterpreted what TC was saying.
Last edited on
You are getting 0 because you are dividing 2 ints. You could either make the const values floats or do batAvg = (float)HITS / AT_BAT;
I need to use a typecast though i just read it on the instructions. How would i do that?
batAvg = (float)HITS / AT_BAT;

The (float) is a typecast. This is making sure at least one of the variables is a float so that it will return a float into batAvg. You can also do it the preferred C++ way which is static_cast<float>(HITS) / AT_BAT; .
Oh i see now. Thank you so much!
Topic archived. No new replies allowed.