**In this project I had to write a program which calculates and displays a baseball player's batting average and proficiency. There are an unknown number of players.** In my battinge_average function, I need to be able to add up all the hits and bats inputs as many times as the user wants. For some reason, my hits counter (hits++) is working but my bats counter (bats++) isn't. In my cout statement to show what it gives me, it is usually pretty far off of what it needs to be. Do I need to make a for loop that counts it, I'm pretty confused so any help would be appreciated. Thanks.
You're handling both hits and bats incorrectly.
Yu read a value into hits for a particular game, then add one to it (hits++).
You do the same for bats.
What you want to do is accumulate an overall total.
So you need to read a value into a different variable and then add that into hits.
Same for bats.
I haven't run this code so there could be a minor mistake or two.
I also changed all tabs to spaces as this is generally considered better.
double batting_average(string playerName)
{
int hits = 0,
bats = 0,
game = 0;
char answer = 'Y';
while (answer == 'Y' || answer == 'y')
{
game++;
cout << "Enter the hits for the player " << playerName << " for game # " << game << ": ";
int h;
cin >> h;
while (h < 0)
{
cout << "Please enter a valid number of hits: ";
cin >> h;
}
hits += h;
cout << "Enter the bats for the player " << playerName << " for game # " << game << ": ";
int b;
cin >> b;
while (b < h) // we don't need to test < 0 since this is included in < h (since it can't be < 0)
{
cout << "Please enter a valid number of bats: ";
cin >> b;
}
bats += b;
cout << "Enter Y for for games for " << playerName << " ELSE enter N to quit: ";
cin >> answer;
}
//debugging cout to display bats
cout << hits << ' ' << bats << '\n';
//batting average calculation
double battingaverage = double(hits) / bats;
return battingaverage;
}