need some assistance in breaking code in to class

ok after somet time im now ready into breaking my code apart into 4 class Sports,NBA,NFL,MLB,


now i have broken it to where that parent class will be the Sports class
and the other 3 classes will drive from Sports the only problem is how i wrote my program and the problem is with the opening and closing of the file i defined in the runSportsSearch function i made .
i made these functions in my Sports.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Match
{
	string Season;
	string Team1;
	string Team2;
	string Result;
};
class Sports
{
public :
void SearchMatches(Match* matches,int size,string TeamName);
virtual void SearchMatches(Match* matches,int size,string TeamName, int record);
int runSportsSearch();
voidWriteLastSearchWord(string word);
void ReadLastSearchWord(std::string &team, int &record);
setTextName();
Sports();
} 

i defined them like this :

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
void sports::WriteLastSearchWord(string word)
{
    ofstream myfile;
    myfile.open(solo);
    myfile << word;
    myfile.close();
}

void Sports::ReadLastSearchWord(std::string &team, int &record)
{
	ifstream inFile(solo);
	getline(inFile, team);
	inFile >> record;
	inFile.close();
}
void Sports::setTextName(char *name3)
{
	solo = name3 ;// sets variable name for one text file

}
void Sports::SearchMatches(Match *matches, int size, std::string TeamName, int record)
{
      //this fuction is to overload the Searchmatches to store data of last team entered      

	//declare the result array
	Match SearchResult;
	//initalize the result counter
	int SearchResultCount=0;

	//int SelectedNumber;

	//search within the matches array
	for(int i=0;i<size;i++)
	{
		//check if the entered name is the team1 or team2 in the match
		if(matches[i].Team1==TeamName || matches[i].Team2==TeamName)
			{
				//update the search result counter
				SearchResultCount++;
				if (SearchResultCount == record)
				{
					//store the 
				//display the seasons
				//cout<<endl<<"("<<SearchResultCount<<") "<<matches[i].Season;
				
			     //store the matched match in the search result array
				SearchResult = matches[i];
					break;
				}
			}
	}
	cout<<endl;

	//check for results
	if(SearchResultCount>0)
	{
		//display the match information
		cout << SearchResult.Season << endl;
		cout <<SearchResult.Team1   << endl;
		cout <<"Over"<<endl;
		cout << SearchResult.Team2  << endl;
		cout << SearchResult.Result << endl;
	}
	else cout<<"No search result";
	cout<<endl;
}
void Sports::SearchMatches(Match *matches, int size, std::string TeamName)
{	

	
	//declare the result array
	Match SearchResult[2000];
	//initalize the result counter
	int SearchResultCount=0;

	int SelectedNumber;

	//search within the matches array
	for(int i=0;i<size;i++)
	{
		//check if the entered name is the team1 or team2 in the match
		if(matches[i].Team1==TeamName || matches[i].Team2==TeamName)
			{
				//update the search result counter
				SearchResultCount++;
				//display the seasons
				cout<<endl<<"("<< SearchResultCount << ") "<<matches[i].Season;
				//store the matched match in the search result array
				SearchResult[SearchResultCount] = matches[i];
			}
	}
	cout << endl;

	//check for results
	if(SearchResultCount>0)

	{
		
		
		cout << "which year would you like to view results "<<endl;
		cin >> SelectedNumber;

		SelectedNumber;
		
	    ofstream myfile;
	    myfile.open("recallTeam.txt",ios::app);
	    myfile << endl << SelectedNumber;
	    myfile.close();
		
		//display the match information
		cout << SearchResult[SelectedNumber].Season<< endl;
		cout << SearchResult[SelectedNumber].Team1 << endl;
		cout << "Over" << endl;
		cout << SearchResult[SelectedNumber].Team2 << endl;
		cout << SearchResult[SelectedNumber].Result << endl;
	}
	else cout << "No results found ";
	cout << endl;
}
Sports::int runSportsSearch()
{
        {
	ifstream inFile("nfl.txt");//want to remove file nfl.txt 
 //so the user
 can make a selection to what file they want open (such as nfl.txt,mlb.txt 
//or nba.txt 
	//initailze the matches ( array of strcuts )
	Match matches[2000];

	// create input stream to nba.txt file
	

	//It will holds the read lines from the file
	string Buffer;

	// check if the file is exist 
	if(!inFile.is_open())
	{
		//display the error message
		cout<<"Failed to open file"<<endl;
		return 0;
	}

	// initalize the counter
	int matchesCounter=0;
	while(!inFile.eof())
	{
		//read season
		getline(inFile,Buffer);
		matches[matchesCounter].Season = Buffer;
		//read team1 name
		getline(inFile,Buffer);
		matches[matchesCounter].Team1 = Buffer;
		//read the "over" string
		getline(inFile,Buffer);
		//read team2 name
		getline(inFile,Buffer);
		matches[matchesCounter].Team2 = Buffer;
		//read result
		getline(inFile,Buffer);
		matches[matchesCounter].Result = Buffer;
		//read the empty line
		getline(inFile,Buffer);
		//update the matches counter
		matchesCounter++;
	}

	
	//its used for Main menu selection
	char Check='1';

	while(Check !='3')
	{
		//Display the main menu
		DisplayMainMenu();
		cin>>Check;

		switch(Check)
		{
			//Search 
		case '1':
			{
				string TeamName="";
				
				getline(cin,TeamName);
				system("cls");
				cout << "Please Enter Team City And Team Name" <<endl;
				getline(cin,TeamName);//free's buffer
				WriteLastSearchWord(TeamName);
				SearchMatches(matches,matchesCounter,TeamName);
				break;
			}
			//Display the last search team
		case '2':
			{
				string team;
				int record;
				ReadLastSearchWord(team, record);
				system("cls");
				cout << "Last Recorded Viewed" <<endl;
				SearchMatches(matches,matchesCounter ,team , record);
	           
				break;
			}
			//Exit the program
		case '3':
			{
				exit(0);
			}
		default:continue;
		
		}
	}
}


my problem is how can i store a variable to hold a temporary value in the inFile until the user prompts which text will be sent from 1 of the other 3 classes to inFile i made coments to where file is ..i only have one problem which is inFile is an undeclared variable ..the other classes will all call the same functions from the sports class the only difference will be what file they open
so what my goal is to define the runSportsSearch Function and call it in each of my new class of MLB,NBA, & NFL so when it is called out will open the proper file text file to open and then run the rest of the code
Last edited on
here is the original code
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// definition of strct that will hold all information of match
struct Match
{
	string Season;
	string Team1;
	string Team2;
	string Result;
};

// Display the main menu on screen
void DisplayMainMenu()
{
	cout<<"1- Please eneter team name"<<endl;
	cout<<"2- View last record"<<endl;
	cout<<"3- Exit program"<<endl;
	cout<<"Please select number"<<endl;
}

//Search within array of matches and display the results
void SearchMatches(Match* matches,int size,string TeamName)
{
	//declare the result array
	Match SearchResult[2000];
	//initalize the result counter
	int SearchResultCount=0;

	int SelectedNumber;

	//search within the matches array
	for(int i=0;i<size;i++)
	{
		//check if the entered name is the team1 or team2 in the match
		if(matches[i].Team1==TeamName || matches[i].Team2==TeamName)
			{
				//update the search result counter
				SearchResultCount++;
				//display the seasons
				cout<<endl<<"("<<SearchResultCount<<") "<<matches[i].Season;
				//store the matched match in the search result array
				SearchResult[SearchResultCount] = matches[i];
			}
	}
	cout<<endl;

	//check for results
	if(SearchResultCount>0)

	{
		
		
		cout << "which year would you like to view results "<<endl;
		cin>>SelectedNumber;

		SelectedNumber;
		
	ofstream myfile;
	myfile.open("recallTeam.txt",ios::app);
	myfile << endl << SelectedNumber;
	myfile.close();
		
		//display the match information
		cout<<SearchResult[SelectedNumber].Season<<endl;
		cout<<SearchResult[SelectedNumber].Team1<<endl;
		cout<<"Over"<<endl;
		cout<<SearchResult[SelectedNumber].Team2<<endl;
		cout<<SearchResult[SelectedNumber].Result<<endl;
	}
	else cout<<"No search result";
	cout<<endl;
}
void SearchMatches(Match* matches,int size,string TeamName, int record)
{
	//declare the result array
	Match SearchResult;
	//initalize the result counter
	int SearchResultCount=0;

	//int SelectedNumber;

	//search within the matches array
	for(int i=0;i<size;i++)
	{
		//check if the entered name is the team1 or team2 in the match
		if(matches[i].Team1==TeamName || matches[i].Team2==TeamName)
			{
				//update the search result counter
				SearchResultCount++;
				if (SearchResultCount == record)
				{
					//store the 
				//display the seasons
				//cout<<endl<<"("<<SearchResultCount<<") "<<matches[i].Season;
				
			     //store the matched match in the search result array
				SearchResult = matches[i];
					break;
				}
			}
	}
	cout<<endl;

	//check for results
	if(SearchResultCount>0)
	{
		//display the match information
		cout<<SearchResult.Season<<endl;
		cout<<SearchResult.Team1<<endl;
		cout<<"Over"<<endl;
		cout<<SearchResult.Team2<<endl;
		cout<<SearchResult.Result<<endl;
	}
	else cout<<"No search result";
	cout<<endl;
}

// This function is used for storing the last search keyword entered to the recallteam.txt
void WriteLastSearchWord(string word)
{
    ofstream myfile;
    myfile.open("recallTeam.txt");
    myfile <<word;
    myfile.close();
}
// This function is used for read the last search keyword enter from the recallteam.txt
void ReadLastSearchWord(string &team, int &record)
{
	ifstream inFile("recallTeam.txt");
	getline(inFile, team);
	inFile >> record;
	inFile.close();
}


int main()
{
	//initailze the matches ( array of strcuts )
	Match matches[2000];

	// create input stream to nba.txt file
	ifstream inFile("nfl.txt");

	//It will holds the read lines from the file
	string Buffer;

	// check if the file is exist 
	if(!inFile.is_open())
	{
		//display the error message
		cout<<"Failed to open file"<<endl;
		return 0;
	}

	// initalize the counter
	int matchesCounter=0;
	while(!inFile.eof())
	{
		//read season
		getline(inFile,Buffer);
		matches[matchesCounter].Season = Buffer;
		//read team1 name
		getline(inFile,Buffer);
		matches[matchesCounter].Team1 = Buffer;
		//read the "over" string
		getline(inFile,Buffer);
		//read team2 name
		getline(inFile,Buffer);
		matches[matchesCounter].Team2 = Buffer;
		//read result
		getline(inFile,Buffer);
		matches[matchesCounter].Result = Buffer;
		//read the empty line
		getline(inFile,Buffer);
		//update the matches counter
		matchesCounter++;
	}

	
	//its used for Main menu selection
	char Check='1';

	while(Check !='3')
	{
		//Display the main menu
		DisplayMainMenu();
		cin>>Check;

		switch(Check)
		{
			//Search 
		case '1':
			{
				string TeamName="";
				getline(cin,TeamName);//users input
				getline(cin,TeamName);//free buffer
				WriteLastSearchWord(TeamName);
				SearchMatches(matches,matchesCounter,TeamName);
				break;
			}
			//Display the last search team
		case '2':
			{
				string team;
				int record;
				ReadLastSearchWord(team, record);
				SearchMatches(matches,matchesCounter ,team , record);
	           
				break;
			}
			//Exit the program
		case '3':
			{
				exit(0);
			}
		default:continue;
		
		}
	}
}
Ok. Quick and Dirty for you. This overrides the parent class, opens the file you want and then calls the parent function of the same name to continue processing. Sound good?

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
class C1 {
public:
  C1() { };
  virtual ~C1() { };
  virtual void saveStuff() { 
    if (!file.is_open())
      file.open("generic_file");
    
    // write crap
  };

protected:
  ifstream  file;
};

class C2 : public C1 {
public:
  C2() { }
  virtual ~C2() { };
  
  void saveStuff() { 
    file.open("file");
    
    // Call Parent Implementation
    C1::saveStuff();    
  }  
};
Last edited on
So basically would just enter this in my parent class so when i go to to my code in sports.cpp this will call the c:1 and c2 functions in my run sports search ..correct ?
To execute this correctly you can do it 2 ways.

1
2
3
4
5
6
7
8
9
int main() {
 C1 myC1 = C1();
 C1.saveStuff(); // This writes it too 'generic_file'

 C2 myC2 = C2();
 C2.saveStuff(); // This writes it to 'file'

 return 0;
}


You want to implement this functionality into your classes. C1 would represent your Sports class, C2 represents the children (NBL, NHL etc)

http://en.wikipedia.org/wiki/Virtual_function :)

Z.
Last edited on
Topic archived. No new replies allowed.