1

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!!!

David Robinson
  • 77,383
  • 16
  • 167
  • 187
PulsePanda
  • 1,806
  • 10
  • 33
  • 56

2 Answers2

4

You are performing integer divisions. Explicitly cast at least one of the operands to a double. For instance:

battingAverage = (static_cast<float>(hits) / (timesBatted - walks));

Same thing for the assignment to sluggingPercentage.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • 3
    [I don’t think we should encourage (or even mention) C-style casts.](http://stackoverflow.com/a/32224/1968) – Konrad Rudolph Mar 06 '13 at 15:41
  • well, that worked really well. i never thought of that XD. thanks for the quick answers!!! i'll accept this one when i can :D – PulsePanda Mar 06 '13 at 15:41
0

Simple as dividing an int by an int is another int. Simply cast one to double.

For example,

battingAverage = static_cast<double>(hits) / (timesBatted - walks)
sluggingPercentage = static_cast<double>(singles + doubles * 2 + triples * 3 + homeRuns*4) / (timesBatted - walks)

Always use C++ casts (static_cast<double>()) rather than C casts (double)(), as the compiler will give you more hints about when you're doing something wrong.

PS Don't hate C++! :( Show it a little love and it will love you back!

Alex Chamberlain
  • 4,147
  • 2
  • 22
  • 49