Help with baseball project

**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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

double batting_average(string playerName);
string proficiency(double average);

int main()
{
	string playerName,		
	playerproficiency;
	
	double average;			
	int answer = 0;

	cout << "Enter the first player's full name: ";
	getline (cin, playerName);

	
	while (answer != 123)
	{
		average = batting_average(playerName);		
		playerproficiency = proficiency(average);
	
		cout << fixed << showpoint << setprecision(3) << "Player " << playerName << "'s batting average is " << average << " and thus his proficiency is: " << playerproficiency << endl;
		cout << "Enter another player's name or enter 123 to quit: ";
		getline(cin, playerName);
		cin.ignore();

	}
	

	return 0;
}

double batting_average(string playerName)
{
	float battingaverage;	
	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 << ": ";
		cin >> hits;
		hits++;	

		
		while (hits < 0)
		{
			cout << "Please enter a valid number of hits: ";
			cin >> hits;
		}

		cout << "Enter the bats for the player " << playerName << " for game # " << game << ": ";
		cin >> bats;
		bats++;	

		
		while (bats < 0 || bats < hits)
		{
			cout << "Please enter a valid number of bats: ";
			cin >> bats;
		}

		cout << "Enter Y for for games for " << playerName << " ELSE enter N to quit: ";
		cin >> answer;
	}

        //debugging cout to display bats
	cout << hits << " " << bats << endl;

        //batting average calculation
	battingaverage = ((double)hits / bats);

	return battingaverage;
}

string proficiency(double average)
{
	
	string proficiency;
	if (average >= 0.000 && average < 0.250)
	{
		proficiency = "Minor Leaguer";
	}
	else if (average >= .250 && average < .300)
	{
		proficiency = "All Star";
	}
	else if (average >= .300 && average < .400)
	{
		proficiency = "Hall of Famer";
	}
	else
	{
		proficiency = "King of Baseball";
	}
	return proficiency;
}
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
}

thank you for your help! I've been stuck on this for a while and it was because I didn't use an accumulate. Have a good rest of your day!
Topic archived. No new replies allowed.