Sign in to follow this  
shadowfire36

array search help

Recommended Posts

ok im trying to build my first dynamic array .. it function is suppose to be able to search for team names but when i type in certain teams i cant seem to get a response like if you type the Chicago Cubs my program says no search found im uploading my code and the file it is suppose to open can anyone help figure out why i cant ready certain team names ? is my array out of range ?
#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 enter 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[100];
	//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;
		
		//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;
}

// 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
string ReadLastSearchWord()
{
	ifstream inFile("recallTeam.txt");
	string Buffer;
	getline(inFile,Buffer);
	return Buffer;
}


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

	// create input stream to nba.txt file
	ifstream inFile("mlb.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);
				getline(cin,TeamName);
				WriteLastSearchWord(TeamName);
				SearchMatches(matches,matchesCounter,TeamName);
				break;
			}
			//Display the last search team
		case '2':
			{
				cout<<ReadLastSearchWord()<<endl;
				break;
			}
			//Exit the program
		case '3':
			{
				exit(0);
			}
		default:continue;
		
		}
	}
}

contents of mlb.txt
2007 	
Boston Red Sox
over 
Colorado Rockies
4-0

2006 
St. Louis Cardinals
over 	
Detroit Tigers
4-1

2005
Chicago White Sox
over
Houston Astros
4-0

2004 
Boston Red Sox
over
St. Louis Cardinals
4-0

2003 
Florida Marlins
over
New York Yankees 
4-2

2002
Anaheim Angels
over
San Francisco Giants 
4-3

2001
Arizona Diamondbacks
over
New York Yankees
4-3

2000
New York Yankees 
over
New York Mets
4-1

1999
New York Yankees
over
Atlanta Braves
4-0

1998 
New York Yankees 
over
San Diego Padres
4-0

1997
Florida Marlins
over
Cleveland Indians 	
4-3

1996 
New York Yankees
over
Atlanta Braves
4-2

1995
Atlanta Braves 
over
Cleveland Indians
4-2

1993 
Toronto Blue Jays
over
Philadelphia Phillies
4-2

1992 
Toronto Blue Jays
over
Atlanta Braves 
4-2

1991
Minnesota Twins
over
Atlanta Braves 
4-3

1990
Cincinnati Reds 
over
Oakland Athletics 
4-0

1989
Oakland Athletics
over
San Francisco Giants 
4-0

1988
Los Angeles Dodgers
over
Oakland Athletics
4-1

1987
Minnesota Twins 
over
St. Louis Cardinals
4-3

1986
New York Mets 
over
Boston Red Sox
4-3

1985
Kansas City Royals
over
St. Louis Cardinals
4-3

1984 	
Detroit Tigers
over
San Diego Padres
4-1

1983
Baltimore Orioles
over
Philadelphia Phillies
4-1

1982
St. Louis Cardinals
over
Milwaukee Brewers 	
4-3

1981 
Los Angeles Dodgers 
over
New York Yankees
4-2

1980
Philadelphia Phillies
over
Kansas City Royals
4-2

1979 
Pittsburgh Pirates
over
Baltimore Orioles 
4-3

1978
New York Yankees
over
Los Angeles Dodgers
4-2

1977
New York Yankees 
over
Los Angeles Dodgers 
4-2

1976
Cincinnati Reds
over
New York Yankees
4-0

1975
Cincinnati Reds
over
Boston Red Sox
4-3

1974
Oakland Athletics
over
Los Angeles Dodgers
4-1

1973
Oakland Athletics
over
New York Mets
4-3

1972
Oakland Athletics
over
Cincinnati Reds
4-3

1971
Pittsburgh Pirates
over
Baltimore Orioles 
4-3

1970
Baltimore Orioles
over
Cincinnati Reds
4-1

1969
New York Mets
over
Baltimore Orioles 
4-1

1968
Detroit Tigers
over
St. Louis Cardinals 
4-3

1967
St. Louis Cardinals
over
Boston Red Sox
4-3

1966
Baltimore Orioles 
over
Los Angeles Dodgers
4-0


1965 
Los Angeles Dodgers
over
Minnesota Twins
4-3

1964
St. Louis Cardinals
New York Yankees
4-3

1963
Los Angeles Dodgers
over
New York Yankees 	
4-0

1962
New York Yankees
over
San Francisco Giants
4-3

1961
New York Yankees
over
Cincinnati Reds
4-1

1960
Pittsburgh Pirates
over
New York Yankees
4-3

1959
Los Angeles Dodgers 
over
Chicago White Sox
4-2

1958
New York Yankees 
over
Milwaukee Braves
4-3

1957
Milwaukee Braves
over
New York Yankees
4-3

1956
New York Yankees 
over
Brooklyn Dodgers
4-3

1955
Brooklyn Dodgers
over
New York Yankees 
4-3

1954
New York Giants
over
Cleveland Indians
4-0

1953
New York Yankees
over
Brooklyn Dodgers
4-2

1952
New York Yankees 
over
Brooklyn Dodgers 
4-3

1951
New York Yankees
over
New York Giants 
4-2

1950
New York Yankees
over
Philadelphia Phillies
4-0

1949
New York Yankees
over
Brooklyn Dodgers 	4-1

1948
Cleveland Indians 
over
Boston Braves
4-2

1947
New York Yankees 
over
Brooklyn Dodgers
4-3

1946
St. Louis Cardinals
over
Boston Red Sox
4-3

1945
Detroit Tigers
over
Chicago Cubs 
4-3

1944
St. Louis Cardinals
over
St. Louis Browns
4-2

1943
New York Yankees
over
St. Louis Cardinals
4-1

1942
St. Louis Cardinals
over
New York Yankees
4-1

1941
New York Yankees 
over
Brooklyn Dodgers
4-1

1940
Cincinnati Reds 
over
Detroit Tigers
4-3

1939
New York Yankees
over
Cincinnati Reds
4-0

1938
New York Yankees 
over
Chicago Cubs
4-0

1937 
New York Yankees 
over
New York Giants
4-1

1936
New York Yankees
over
New York Giants 
4-2

1935
Detroit Tigers
over
Chicago Cubs
4-2

1934
St. Louis Cardinals
over
Detroit Tigers
4-3

1933
New York Giants
over
Washington Senators 
4-1

1932
New York Yankees
over
Chicago Cubs
4-0

1931
St. Louis Cardinals
over
Philadelphia Athletics
4-3

1930
Philadelphia Athletics
over
St. Louis Cardinals 	
4-2

1929
Philadelphia Athletics
over
Chicago Cubs
4 -1

1928
New York Yankees
over
St. Louis Cardinals
4-0

1927
New York Yankees
over
Pittsburgh Pirates
4-0

1926
St. Louis Cardinals
over
New York Yankees 	4-3

1925
Pittsburgh Pirates
over
Washington Senators 
4-3

1924
Washington Senators
over
New York Giants
4-3

1923
New York Yankees
over
New York Giants
4-2

1922
New York Giants
over
New York Yankees
4-0

1921
New York Giants
over
New York Yankees
5-3

1920
Cleveland Indians
over
Brooklyn Dodgers 
5-2

1919 
Cincinnati Reds
over
Chicago White Sox
5-3

1918
Boston Red Sox 
over
Chicago Cubs
4-2

1917
Chicago White Sox
over
New York Giants
4-2

1916
Boston Red Sox 
over
Brooklyn Dodgers
4-1

1915
Boston Red Sox
over
Philadelphia Phillies
4-1

1914
Boston Braves
over
Philadelphia Athletics
4-0

1913
Philadelphia Athletics
over
New York Giants
4-1

1912
Boston Red Sox
over
New York Giants
4-3

1911
Philadelphia Athletics
over
New York Giants
4-2

1910
Philadelphia Athletics
over
Chicago Cubs 
4-1

1909
Pittsburgh Pirates
over
Detroit Tigers
4-3

1908
Chicago Cubs
over
Detroit Tigers
4-1

1907
Chicago Cubs
over
Detroit Tigers
4-0

1906
Chicago White Sox 
over
Chicago Cubs
4-2

1905
New York Giants
over
Philadelphia Athletics
4-1

1903
Boston Red Sox
over
Pittsburgh Pirates
5-3

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this