Baseball Stats program compiles with errors

Jun 5, 2013 at 6:36pm
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 Jun 5, 2013 at 6:42pm
Jun 5, 2013 at 6:38pm
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 Jun 5, 2013 at 6:40pm
Jun 5, 2013 at 6:43pm
Your right Vlad, I worded that incorrectly. I corrected my post. Thanks for pointing that out.
Jun 5, 2013 at 6:47pm
Post your compiler errors
Jun 5, 2013 at 7:01pm
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 ==========
Jun 5, 2013 at 7:04pm
Can you read what others write to you? Reread my previous post.
Last edited on Jun 5, 2013 at 7:04pm
Jun 5, 2013 at 7:13pm
Yes I read your post, but I don't understand what you mean? You have no idea what is BaseballPlayer??
Jun 5, 2013 at 7:15pm
.\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.
Jun 5, 2013 at 7:47pm
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.
Jun 5, 2013 at 8:08pm
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.