Baseball Stats program compiles with errors

Hello all, I am a noob in the process of learning C++ and so far things are going good. However I wrote a pretty simple Baseball statistics program from out of my book today and it keeps compiling with errors but I can't understand what is wrong with my code. If anyone would be willing to look at my code and give me a hint as to what I am missing it would be greatly appreciated.

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
 // BaseballStats.cpp : main project file.

#include "stdafx.h"
#include<iostream>

using namespace std;

int main()
{
    int lowAtBats;
	int highAtBats;
	int atBats;
	BaseballPlayer onePlayer;
	cout << "Please enter the baseball players number ";
	cin >> onePlayer.playerNumber;
	cout << "Now enter the number of hits for the player #" << onePlayer.playerNumber << " ";
	cin >> onePlayer.numberOfHits;
	cout << "\nNext enter the low number of at bats for which you want statistics ";
	cin >> lowAtBats;
	while(lowAtBats < onePlayer.numberOfHits)
	{
		cout << "***Player had " << onePlayer.numberOfHits << " hits, so you must enter that number or more" << endl;
		cout << "   Please reenter the low number of at bats ";
		cin >> lowAtBats;
	}
	cout << "\nEnter the most at bats for which you want statistics ";
	cin >> highAtBats;
	while(highAtBats < lowAtBats)
	{
		cout<<"***You entered " << lowAtBats <<
			" for the low number of at bats" << endl << " so you must enter that number or more" << endl;
	cout << "   Please reenter a number for most at bats ";
	cin >> highAtBats;
	}
	cout<<"\nFor player #" << onePlayer.playerNumber << endl;
	atBats = lowAtBats;
	while(atBats <= highAtBats)
	{
		cout<< onePlayer.numberOfHits << " hits in " << atBats << 
			" at bats is an average of " <<
			(onePlayer.numberOfHits * 1.0) / atBats << endl;
		++atBats;
	}
	system("pause");
    return 0;
}
Last edited on
It is the compiler that looks at your code and reports errors.

As for us then we have no any idea what is BaseballPlayer
Last edited on
Your right Vlad, I worded that incorrectly. I corrected my post. Thanks for pointing that out.
Post your compiler errors
These are the errors I am getting:

1>------ Build started: Project: BaseballStats, Configuration: Debug Win32 ------
1>Compiling...
1>BaseballStats.cpp
1>.\BaseballStats.cpp(13) : error C2065: 'BaseballPlayer' : undeclared identifier
1>.\BaseballStats.cpp(13) : error C2146: syntax error : missing ';' before identifier 'onePlayer'
1>.\BaseballStats.cpp(13) : error C2065: 'onePlayer' : undeclared identifier
1>.\BaseballStats.cpp(15) : error C2065: 'onePlayer' : undeclared identifier
1>.\BaseballStats.cpp(15) : error C2228: left of '.playerNumber' must have class/struct/union
1> type is ''unknown-type''
1>.\BaseballStats.cpp(16) : error C2065: 'onePlayer' : undeclared identifier
1>.\BaseballStats.cpp(16) : error C2228: left of '.playerNumber' must have class/struct/union
1> type is ''unknown-type''
1>.\BaseballStats.cpp(17) : error C2065: 'onePlayer' : undeclared identifier
1>.\BaseballStats.cpp(17) : error C2228: left of '.numberOfHits' must have class/struct/union
1> type is ''unknown-type''
1>.\BaseballStats.cpp(20) : error C2065: 'onePlayer' : undeclared identifier
1>.\BaseballStats.cpp(20) : error C2228: left of '.numberOfHits' must have class/struct/union
1> type is ''unknown-type''
1>.\BaseballStats.cpp(20) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://c:\Users\Dredwerx\Documents\SHC & other College Material\PRG105 C#\C# Projects\BaseballStats\Debug\BuildLog.htm"
1>BaseballStats - 12 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Can you read what others write to you? Reread my previous post.
Last edited on
Yes I read your post, but I don't understand what you mean? You have no idea what is BaseballPlayer??
.\BaseballStats.cpp(13) : error C2065: 'BaseballPlayer' : undeclared identifier


Given this error, I'd say the compiler doesn't know either.

The problem is you haven't ever defined what a BasebballPlayer is.
Where have you created the class for BaseballPlayer? It should look like this:

1
2
3
4
5
class Baseballplayer{
 Baseballplayer() {};
 int playerNumber;
 int numberOfHits;
}


If you can't find where the class is defined, go ahead and add this class right above the main().

I expect you to ask if there is something here whose reason you don't understand.
Okay well, I probably just need to finish reading this chapter I am in and then I will go back and see if I can figure out what I am doing wrong. Thanks for the suggestions. I will come back here if I am still stuck later.
Peace out.
Topic archived. No new replies allowed.