Baseball player program

I am having issues getting this program, that should display the name of the player with the highest average and that average, to work correctly, Could anyone help?

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
# include <iostream>
# include <string> 
using namespace std;
int main () {

	string name1,name2,name3; 
	int ab1,ab2,ab3;
	int hits1,hits2,hits3;
	double avg1, avg2, avg3;
	
	//Player 1
cout << "Please enter the player's name:";
cin >> name1; 
cout << "Please enter the official times at bat:";
cin >> ab1; 
	if (ab1 < 0 || ab1 > 25){
		cout << "Invalid"; 
	}
	if (ab1 > 0 && ab1 < 25){
		cout << "Please enter the number of hits:"; 
		cin >> hits1; 
	}
	if (hits1 < 0){
		cout << "Invalid";
	}
	if (hits1 < ab1){
	avg1 = hits1/ab1; 
	}

	//Player 2
cout << "Please enter the player's name:";
cin >> name2; 
cout << "Please enter the official times at bat:";
cin >> ab2; 
	if (ab2 < 0 || ab2 > 25) {
		cout << "Invalid"; 
	}
	if (ab2 > 0 && ab2 < 25){
		cout << "Please enter the number of hits:"; 
		cin >> hits2; 
	}
	if (hits2 < 0){
		cout << "Invalid";
	}
	if (hits2< ab2) {
	avg2 = hits2/ab2; 
	}
	
	//Player 3
cout << "Please enter the player's name:";
cin >> name3; 
cout << "Please enter the official times at bat:";
cin >> ab3; 
	if (ab3 < 0 || ab3 > 25) {
		cout << "Invalid"; 
	}
	if (ab3 > 0 && ab3 < 25){
		cout << "Please enter the number of hits:"; 
		cin >> hits3; 
	}
	if (hits3 < 0){
		cout << "Invalid";
	}
	if (hits3 < ab3){
	avg3 = hits3/ab3; 
	}
	
	//Final Display
	if (avg1 > avg3 && avg1 > avg2){
		cout << "The highest average belongs to" << name1 << "with" << avg1 << endl;
	}
	if (avg2 > avg3 && avg2 > avg1){
		cout << "The highest average belongs to" << name2 << "with" << avg2 << endl;
	}
	if (avg3 > avg2 && avg3 > avg1){
		cout << "The highest average belongs to" << name1 << "with" << avg1 << endl;
	}
	return 0;

it works now, double check again see if the result how you want it to be...
heh

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
#include<iostream>
#include<string> 
using namespace std;

int main() {

	string name1,name2,name3; 
	int ab1,ab2,ab3;
	int hits1,hits2,hits3;
	double avg1, avg2, avg3;
	
	//Player 1
	cout << "Please enter the player's name:";
	cin >> name1; 
	cout << "Please enter the official times at bat:";
	cin >> ab1; 
	if (ab1 < 0 || ab1 > 25)
		cout << "Invalid"; 
	else if (ab1 > 0 && ab1 < 25){
		cout << "Please enter the number of hits:"; 
		cin >> hits1; 
		
		if (hits1 < 0)
			cout << "Invalid";
		else
			avg1 = hits1/ab1; 
	}
	//Player 2
	cout << "Please enter the player's name:";
	cin >> name2; 
	cout << "Please enter the official times at bat:";
	cin >> ab2; 
	if (ab2 < 0 || ab2 > 25)
		cout << "Invalid"; 
	else if (ab2 > 0 && ab2 < 25){
		cout << "Please enter the number of hits:"; 
		cin >> hits2; 
		
		if (hits2 < 0)
			cout << "Invalid";
		else
			avg2 = hits2/ab2;
	}
	//Player 3
		cout << "Please enter the player's name:";
	cin >> name3; 
	cout << "Please enter the official times at bat:";
	cin >> ab3; 
	if (ab3 < 0 || ab3 > 25)
		cout << "Invalid"; 
	else if (ab3 > 0 && ab3 < 25){
		cout << "Please enter the number of hits:"; 
		cin >> hits3; 
		
		if (hits3 < 0)
			cout << "Invalid";
		else
			avg3 = hits3/ab3; 
	}
	
	//Final Display
	if (avg1 > avg3 && avg1 > avg2)
		cout << "The highest average belongs to " << name1 << " with " << avg1 << endl;
	else if (avg2 > avg3 && avg2 > avg1)
		cout << "The highest average belongs to " << name2 << " with " << avg2 << endl;
	else if (avg3 > avg2 && avg3 > avg1)
		cout << "The highest average belongs to " << name1 << " with " << avg1 << endl;

	return 0;

	}
there are some more modifications you'll have to make in order for this to work properly...

first, the divisions hits1/ab1, hits2/ab2, hits3/ab3 will not give you the result you expect. hits and ab variables are all ints, so these are integer divisions that will give an integer as a result. For example 5/2 will give you 2 instead of the desirable 2.5. To fix these you should type the divisions like

avg1=hits1/double(ab1);

or

avg1=double(hits1)/ab1;

or

avg1=double(hits1)/double(ab1);

This will force your compiler to make a double division and thus give you the result you want.

second, a typing error in the end of main, where you write
1
2
else if (avg3 > avg2 && avg3 > avg1)
		cout << "The highest average belongs to " << name1 << " with " << avg1 << endl;


it should be
1
2
else if (avg3 > avg2 && avg3 > avg1)
		cout << "The highest average belongs to " << name3 << " with " << avg3 << endl;


third, if avg1==avg2==avg3 (and in some other similar cases) then your program won't print anything. To avoid this you should use >= instead of > for your comparisons in the end of main.

and finally, add something like cin.get(); or system("pause"); before the return statement so that you can actually see the output of your program.
Last edited on
Topic archived. No new replies allowed.