Little help please

I have a program to write that uses an array to read from 2 different files. The first file is named Teams.txt and displays all the MLB teams that have won the World Series since 1903. The second file is WolrdSeriesWinners.txt and it has all the winners in chronological order from 1903-2013. The first part of the code will open the first file and write the names that the user is to choose from and works just fine. The problem I am having is trying to figure out how to make the second part which will tell the user how many times the team they selected has won. Any help would be appreciated.
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
    #include <iostream>
    #include <fstream>
    #include <string>
    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 array[SIZE]; 
		int i=0; 
		string line; 

		ifstream in ("Teams.txt"); 

		if (in.is_open()) 
			{
				while (! in.eof() ) 
				{

					getline (in,line);

					array[i] = line;

					cout << array[i] << endl;

					i++;
				}
				in.close(); 
			}

	
		string tempstr, team;
		int count, num;
		count = 0;
		num = 0;
		
		ifstream infile ("WorldSeriesWinners.txt");
		
		cout<<"Choose a team from the list and see how many times they have won since 1903.\n";
		getline(cin,team);
		while(infile >> tempstr)
		{
		if(tempstr == team)
		{
		num++;
		}
		count++;
		}
		cout<< "The " << team << " have won the World Series " << num << " times since 1903.\n";
		



    system("PAUSE");
    return 0;
    }
Last edited on
It might be your use of the insertion operator. It will take a string of data up until a space. That means if the user wants to see how many times the "New York Yankees" won the World Series, the insertion operator will read in and subsequently compare each individual world from the file. So while the Yankees have won multiple World Series, the insertion operator will read in like so:
"New", "York", "Yankees". These three individual strings will never be equivalent to "New York Yankees" as a result.

tl;dr you should probably use the getline function when you read in the second file.
I understand what you're saying but I have never used a getline to read from a file before. I used the getline to capture all of the user input for the same reason. Would it looks something like

while(getline(infile, tempstr)) ?
You did use getline to read from a file! ;) Line 22 to be exact.
Thanks for the help I fixed the code and it runs great!
Topic archived. No new replies allowed.