I am doing an assignment for Computer Science. We have to make a batting average program. I have it working so that it will calculate the hits, outs, singles, etc. but when it comes to making it calculate the batting average, it's coming up with 0.000. I have no clue why, I've done a ton of google searching, tried making the variable double and float, etc. here is the code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main(){
const int MAX_TIMES_AT_BAT = 1000;
int hits = 0, timesBatted = 0, outs = 0, walks = 0, singles = 0, doubles = 0, triples = 0, homeRuns = 0;
float battingAverage = 0.0, sluggingPercentage = 0.0;
for(int i = 0; i < MAX_TIMES_AT_BAT; i++){
int random = rand() % 100 +1;
if(random > 0 && random <= 35){
outs++;
}else if(random > 35 && random <= 51){
walks++;
}else if(random > 51 && random <= 71){
singles++;
hits++;
}else if(random > 71 && random <= 86){
doubles++;
hits++;
}else if(random > 86 && random <= 95){
triples++;
hits++;
}else if(random > 95 && random <= 100){
homeRuns++;
hits++;
}else{
cout << "ERROR WITH TESTING RANDOM!!!";
return(0);
}
timesBatted++;
}
cout << timesBatted << " " << hits << " " << outs << " " << walks << " " << singles << " " << doubles << " " << triples << " " << homeRuns << endl;
battingAverage = (hits / (timesBatted - walks));
sluggingPercentage = (singles + doubles * 2 + triples * 3 + homeRuns*4) / (timesBatted - walks);
cout << fixed << setprecision(3) << "Batting Average: " << battingAverage << "\nSlugging Percentage: " << sluggingPercentage << endl;
return 0;
}
Any help would be great! What is going wrong??? I calculated it, and the batting average should be 0.5646 and the slugging percentage should be 1.0937. what its showing is 0.0000, and 1.0000. Thanks in advance!!!