Problem!

Okay this has to be one of the simplest programs I've ever written, but for some reason my end result is always 0, unless at_bats and hits are the same number which in turn makes the outcome 1. Seriously, what is my stupid brain doing wrong?

#include <iostream>
using namespace std;

int main( )
{
int at_bats, hits;
double batting_average;

cout << "How many at bats has the player had? ";
cin >> at_bats;
cout << "How many hits has the player had? ";
cin >> hits;

batting_average = (hits / at_bats);

cout << "The players batting average is, " << batting_average << ".\n";

return 0;
}
You've got at_bats and hits as type int. Change them to doubles, and your problem's solved.
Last edited on
Integer arithmetic is you problem. Your batting_average may be a double, but hits and at_bats are ints, so anything that involves only ints and division is gonna end up being a whole number.

E.g. int 5, 10

10/5 = 2; 5/10 = 0;
Thank you both so much!
Topic archived. No new replies allowed.