ok i finally fixed my code .
the only issue i currently have is that it is case sensitive and since i was using strings i was wonder if there was a way to implement for the system to check upper and lower case letters in the users input field
my current code is upper and lower letter example:Dallas Cowboys or Green Bay
Packers
i wanted a way for the system to check if the user
enter dallas cowboys or GREEN BAY PACKERS ,and still produce the same results as
Dallas Cowboys
here is my final code
#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.Team1==TeamName || matches.Team2==TeamName)
{
//update the search result counter
SearchResultCount++;
//display the seasons
cout<<endl<<"("<<SearchResultCount<<") "<<matches.Season;
//store the matched match in the search result array
SearchResult[SearchResultCount] = matches;
}
}
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.Team1==TeamName || matches.Team2==TeamName)
{
//update the search result counter
SearchResultCount++;
if (SearchResultCount == record)
{
//store the
//display the seasons
//cout<<endl<<"("<<SearchResultCount<<") "<<matches.Season;
//store the matched match in the search result array
SearchResult = matches;
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;
}
}
}
here is the read in nfl.txt
Super Bowl XLII 2008
New York Giants
over
New England Patriots
17–14
Super Bowl XLI 2007
Indianapolis Colts
over
Chicago Bears
29–17
Super Bowl XL 2006
Pittsburgh Steelers
over
Seattle Seahawks
21-10
Super Bowl XXXIX 2005
New England Patriots
over
Philadelphia Eagles
24-21
Super Bowl XXXVIII 2004
New England Patriots
over
Carolina Panthers
32-29
Super Bowl XXXVII 2003
Tampa Bay Buccaneers
over
Oakland Raiders
48-21
Super Bowl XXXVI 2002
New England Patriots
over
St. Louis Rams
20-17
Super Bowl XXXV 2001
Baltimore Ravens
over
New York Giants
34-7
Super Bowl XXXIV 2000
St. Louis Rams
over
Tennessee Titans
23-16
Super Bowl XXXIII 1999
Denver Broncos
over
Atlanta Falcons
34-19
Super Bowl XXXII 1998
Denver Broncos
over
Green Bay Packers
31-24
Super Bowl XXXI 1997
Green Bay Packers
over
New England Patriots
35-21
Super Bowl XXX 1996
Dallas Cowboys
over
Pittsburgh Steelers
27-17
Super Bowl XXIX 1995
San Francisco 49ers
over
San Diego Chargers
49-26
Super Bowl XXVIII 1994
Dallas Cowboys
over
Buffalo Bills
30-13
Super Bowl XXVII 1993
Dallas Cowboys
over
Buffalo Bills
52-17
Super Bowl XXVI 1992
Washington Redskins
over
Buffalo Bills
37-24
Super Bowl XXV 1991
New York Giants
over
Buffalo Bills
20-19
Super Bowl XXIV 1990
San Francisco 49ers
over
Denver Broncos
55-10
Super Bowl XXIII 1989
San Francisco 49ers
over
Cincinnati Bengals
20-16
Super Bowl XXI 1988
Washington Redskins
over
Denver Broncos
42-10
Super Bowl XXI 1987
New York Giants
over
Denver Broncos
39-20
Super Bowl XX 1986
Chicago Bears
over
New England Patriots
46-10
Super Bowl XIX 1985
San Francisco 49ers
over
Miami Dolphins
38-16
Super Bowl XVIII 1984
Los Angeles Raiders
over
Washington Redskins
38- 9
Super Bowl XVII 1983
Washington Redskins
over
Miami Dolphins
27-17
Super Bowl XVI 1982
San Francisco 49ers
over
Cincinnati Bengals
26-21
Super Bowl XV 1981
Oakland Raiders
over
Philadelphia Eagles
27-10
Super Bowl XIV 1980
Pittsburgh Steelers
over
Los Angeles Rams
31-19
Super Bowl XIII 1979
Pittsburgh Steelers
over
Dallas Cowboys
35-31
Super Bowl XII 1978
Dallas Cowboys
over
Denver Broncos
27-10
Super Bowl XI 1977
Oakland Raiders
over
Minnesota Vikings
32-14
Super Bowl X 1976
Pittsburgh Steelers
over
Dallas Cowboys
21-17
Super Bowl IX 1975
Pittsburgh Steelers
over
Minnesota Vikings
16-6
Super Bowl VIII 1974
Miami Dolphins
over
Minnesota Vikings
24-7
Super Bowl VII 1973
Miami Dolphins
over
Washington Redskins
14-7
Super Bowl VI 1972
Dallas Cowboys
over
Miami Dolphins
24-3
Super Bowl V 1971
Baltimore Colts
over
Dallas Cowboys
16-13
Super Bowl IV 1970
Kansas City Chiefs
over
Minnesota Vikings
23-7
Super Bowl III 1969
New York Jets
over
Baltimore Colts
16-7
Super Bowl II 1968
Green Bay Packers
over
Oakland Raiders
33-14
Super Bowl I 1967
Green Bay Packers
over
Kansas City Chiefs
35-10