How can I compare two strings and determine if they are equal?

This is my line of code. In the "worldSeriesChampions.txt" the San Francisco Giants win twice I believe. When I run the program and enter the San Francisco Giants when prompted, the program doesn't seem to match the name from the while loop that I created. Can someone help me please? Thank you!

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;

int main ()
{
cout<<"MLB teams that have won the World Series at least once :\n";
cout<<"========================================================\n";

const int SIZE = 28;
string team[SIZE];

ifstream inputFile;
inputFile.open("Teams.txt");

for (int index = 0; index < SIZE; index++)
{
getline(inputFile, team[index]);

cout << team[index] << endl;
}

inputFile.close();

vector<string> worldChamps;
string yourTeam;
string readTeams, worldChampTeam;
long numValues;
int value = 0;
int num = 0;

ifstream inputFile2;
inputFile2.open("WorldSeriesWinners.txt");

while (getline(inputFile2, readTeams))
{
worldChamps.push_back(readTeams);
}

cout<<"\nChoose a team from the list and see how many times they have won since 1903.\n";
getline(cin, yourTeam);

numValues = worldChamps.size();

while (value < numValues)
{
worldChampTeam = worldChamps[value];

if (worldChampTeam == yourTeam)
num++;
value++;

}

inputFile2.close();

cout << "\nThe " << yourTeam << " have won the World Series " << num << " times since 1903.\n";

return 0;
}
Last edited on
1
2
3
4
5
6
7
string s = "yo";
	string b = "yo";

	if (b == s)
	{
		cout << "working" << endl;
	}
Can you show me where I made a mistake in my program?
It's late so I might have missed something obvious, but no errors jump out at me. I'd start checking for typos or differences in the data between the two files: does one record have "San Francisco Giants" and the other "New York Giants" perhaps, or just "Giants"?

Also, have you checked to make sure your vector is being filled?
1
2
for(int i = 0; i < worldChamps.size(); i++)
   std::cout << worldChamps[i] << std::endl;

If the vector isn't populated, you won't find a match.
Also, are you only having trouble with the Giants or is it all teams?
Let us know what you find out. If it still isn't working I'll look again.
Last edited on
In the "worldSeriesChampions.txt" the San Francisco Giants win twice I believe.
inputFile2.open("WorldSeriesWinners.txt");
Topic archived. No new replies allowed.