need help implmenting on how to check upper and lower

Started by
2 comments, last by Lajnold 15 years, 11 months ago
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&lt;&lt;"1- Please eneter team name"&lt;&lt;endl;
	cout&lt;&lt;"2- View last record"&lt;&lt;endl;
	cout&lt;&lt;"3- Exit program"&lt;&lt;endl;
	cout&lt;&lt;"Please select number"&lt;&lt;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&lt;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&lt;&lt;endl&lt;&lt;"("&lt;&lt;SearchResultCount&lt;&lt;") "&lt;&lt;matches.Season;
				//store the matched match in the search result array
				SearchResult[SearchResultCount] = matches;
			}
	}
	cout&lt;&lt;endl;

	//check for results
	if(SearchResultCount&gt;0)

	{
		
		
		cout &lt;&lt; "which year would you like to view results "&lt;&lt;endl;
		cin&gt;&gt;SelectedNumber;

		SelectedNumber;
		
	ofstream myfile;
	myfile.open("recallTeam.txt",ios::app);
	myfile &lt;&lt; endl &lt;&lt; SelectedNumber;
	myfile.close();
		
		//display the match information
		cout&lt;&lt;SearchResult[SelectedNumber].Season&lt;&lt;endl;
		cout&lt;&lt;SearchResult[SelectedNumber].Team1&lt;&lt;endl;
		cout&lt;&lt;"Over"&lt;&lt;endl;
		cout&lt;&lt;SearchResult[SelectedNumber].Team2&lt;&lt;endl;
		cout&lt;&lt;SearchResult[SelectedNumber].Result&lt;&lt;endl;
	}
	else cout&lt;&lt;"No search result";
	cout&lt;&lt;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&lt;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&lt;&lt;endl&lt;&lt;"("&lt;&lt;SearchResultCount&lt;&lt;") "&lt;&lt;matches.Season;
				
			     //store the matched match in the search result array
				SearchResult = matches;
					break;
				}
			}
	}
	cout&lt;&lt;endl;

	//check for results
	if(SearchResultCount&gt;0)
	{
		//display the match information
		cout&lt;&lt;SearchResult.Season&lt;&lt;endl;
		cout&lt;&lt;SearchResult.Team1&lt;&lt;endl;
		cout&lt;&lt;"Over"&lt;&lt;endl;
		cout&lt;&lt;SearchResult.Team2&lt;&lt;endl;
		cout&lt;&lt;SearchResult.Result&lt;&lt;endl;
	}
	else cout&lt;&lt;"No search result";
	cout&lt;&lt;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 &lt;&lt;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 &gt;&gt; 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&lt;&lt;"Failed to open file"&lt;&lt;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&gt;&gt;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

Advertisement
You could always use tolower() to convert your input to all lower case, and then run an algorithm over your line of input that calls toupper() on the first letter of each word.
If you want to perform a case insensitive comparison of two strings A and B, convert all letters in both strings to upper or lower case, then compare them for equivalence.

#include <cctype> // for toupper() or tolower()void stringToUpper(char* str){  while (*str)  {    *str = toupper(*str);    ++str;  }}int caseInsensitveCompare(char* A, char* B){    stringToUpper(A);    stringToUpper(B);    return strcmp(A, B);}


[Edited by - fpsgamer on May 12, 2008 9:19:23 PM]
Know that fpsgamer's solution can also be applied to std::string.

#include <string>#include <cctype>void stringToUpper(std::string &str){	for(std::string::iterator iter = str.begin(); iter != str.end(); iter++)		*iter = std::toupper(*iter);}bool caseInsensitveCompare(std::string A, std::string B){	stringToUpper(A);	stringToUpper(B);	return A == B;}


There is also the transform function in the algorithm header that can be easier/shorter to use than writing a loop to transform the elements of the string.

#include <string>#include <algorithm>#include <cctype>std::string stringToUpper(std::string &str){	std::transform(str.begin(), str.end(), str.begin(), std::toupper);}bool caseInsensitveCompare(std::string A, std::string B){	stringToUpper(A);	stringToUpper(B);	return A == B;}


Although both these solutions require copying the strings. One solution could be to loop over the elements to compare them, using toupper/tolower when comparing.

bool caseInsensitiveCompare(const std::string &A, const std::string &B){	if(A.size() != B.size())		return false;	for(std::string::size_type i = 0; i < A.size(); i++)		if(std::toupper(A) != std::toupper(B))			return false;	return true;}


And there might be better solutions.

[Edited by - Lajnold on May 14, 2008 4:15:51 PM]

This topic is closed to new replies.

Advertisement