Read the data from the files and giving the correct answer

I need help understanding what I'm doing wrong in my coding(c++). The program should then display the number of times that team has won the World Series whenever I put a name on the team, the answer is that they have won 116 times. This happens for all the teams that I put in. I'm new in programming and I'm VERY confused. Would you please help me or explain to me what I'm doing wrong?
Here is my code:
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
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <array>
#include <fstream>
using namespace std;

int main()
{
    //variables
    string teams, winners;
    string TeamsList[200] = {};
    string WinnersList[200] = {};
    
    int counter = 0;
    
    //open file Teams
    ifstream teamsFile;
    teamsFile.open("Teams.txt");
    
    //If it does not open display "error"
    if(!teamsFile){
        cout << "error" << endl;
        return 0;
    }
    // If it does open display the content of the file
    cout << "Team won World Series: \n";
    while (getline(teamsFile, teams)){
        cout << teams << endl;
    }
    //close file
    teamsFile.close();
    
    //ask for a input
    cout << "Enter team name to see if win World Series: \n";
    getline(cin, teams);
    
    bool found = false;
    
    // Search for the input
    for (int i = 0; i < winners.size(); i++){
        if(TeamsList[i] == teams){
            found = true;
            break;
        }
    }
    //open the winners file
    ifstream winnersFile;
    winnersFile.open("WorldSeriesWinners.txt");
    
    // If it does not open display "error"
    if(!winnersFile){
        cout << "error" << endl;
        return 0;
    }
    // If it does open count the number of times that the team won
    while(getline(winnersFile, winners)){
        if(winners == winners)
            counter++;
    }
    //display result
    cout << teams << " has won the world series " << counter << " times. \n ";
    
    winnersFile.close();
    
    return 0;
}



Last edited on
> Winners[Winner++] = name;
This isn't the thing you just read in.

> if(winner == name)
If you're off by even a single character, the comparison will fail.

There's nothing to stop you adding debug code.

1
2
3
4
5
6
    while(getline(inputFile, winner)){
        Winners[Winner++] = winner;  //!! fixed
        cout << "DEBUG: compare >" << winner << "< with >" << name << "<\n";
        if(winner == name)
            counter++;
    }

Then look at your debug messages, especially the ones which you thought should have compared equal.
What's the point of L 40 - 42? It does nothing. Don't you need to display a message here if the entered team name is found?

What's the format of the 2 text files? A simple list of names? Are there any spaces at the end of the lines - as these will cause the comparison to fail. Yo might need to remove trailing white-space chars.

the formt of the file are ".txt" so only have a bunch of name on it

WorldSeriesWinners.txt
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
 Boston Americans
New York Giants
Chicago White Sox
Chicago Cubs
Chicago Cubs
Pittsburgh Pirates
Philadelphia Athletics
Philadelphia Athletics
Boston Red Sox
Philadelphia Athletics
Boston Braves
Boston Red Sox
Boston Red Sox
Chicago White Sox
Boston Red Sox
Cincinnati Reds
Cleveland Indians
New York Giants
New York Giants
New York Yankees
Washington Senators
Pittsburgh Pirates
St. Louis Cardinals
New York Yankees
New York Yankees
Philadelphia Athletics
Philadelphia Athletics
St. Louis Cardinals
New York Yankees
New York Giants
St. Louis Cardinals
Detroit Tigers
New York Yankees
New York Yankees
New York Yankees
New York Yankees
Cincinnati Reds
New York Yankees
St. Louis Cardinals
New York Yankees
St. Louis Cardinals
Detroit Tigers
St. Louis Cardinals
New York Yankees
Cleveland Indians
New York Yankees
New York Yankees
New York Yankees
New York Yankees
New York Yankees
New York Giants
Brooklyn Dodgers
New York Yankees
Milwaukee Braves
New York Yankees
Los Angeles Dodgers
Pittsburgh Pirates
New York Yankees
New York Yankees
Los Angeles Dodgers
St. Louis Cardinals
Los Angeles Dodgers
Baltimore Orioles
St. Louis Cardinals
Detroit Tigers
New York Mets
Baltimore Orioles
Pittsburgh Pirates
Oakland Athletics
Oakland Athletics
Oakland Athletics
Cincinnati Reds
Cincinnati Reds
New York Yankees
New York Yankees
Pittsburgh Pirates
Philadelphia Phillies
Los Angeles Dodgers
St. Louis Cardinals
Baltimore Orioles
Detroit Tigers
Kansas City Royals
New York Mets
Minnesota Twins
Los Angeles Dodgers
Oakland Athletics
Cincinnati Reds
Minnesota Twins
Toronto Blue Jays
Toronto Blue Jays
Atlanta Braves
New York Yankees
Florida Marlins
New York Yankees
New York Yankees
New York Yankees
Arizona Diamondbacks
Anaheim Angels
Florida Marlins
Boston Red Sox
Chicago White Sox
St. Louis Cardinals
Boston Red Sox
Philadelphia Phillies
New York Yankees
San Francisco Giants
St. Louis Cardinals
San Francisco Giants
Boston Red Sox
San Francisco Giants
Kansas City Royals
Chicago Cubs
Houston Astros
Boston Red Sox
Washington Nationals
Los Angeles Dodgers


Teams.txt
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
Anaheim Angels
Arizona Diamondbacks
Atlanta Braves
Baltimore Orioles
Boston Americans
Boston Braves
Boston Red Sox
Brooklyn Dodgers
Chicago Cubs
Chicago White Sox
Cincinnati Reds
Cleveland Indians
Detroit Tigers
Florida Marlins
Houston Astros
Kansas City Royals
Los Angeles Dodgers
Milwaukee Braves
Minnesota Twins
New York Giants
New York Mets
New York Yankees
Oakland Athletics
Philadelphia Athletics
Philadelphia Phillies
Pittsburgh Pirates
San Francisco Giants
St. Louis Cardinals
Toronto Blue Jays
Washington Nationals
Washington Senators
Perhaps:

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
#include <string>
#include <map>
#include <iostream>
#include <fstream>

int main()
{
	std::ifstream fteam("Teams.txt");

	if (!fteam)
		return (std::cout << "Cannot open teams file\n"), 1;

	std::map<std::string, unsigned> teams;

	for (std::string team; std::getline(fteam, team); teams.emplace(team, 0));

	std::ifstream fwins("WorldSeriesWinners.txt");

	if (!fwins)
		return (std::cout << "Cannot open winners file\n"), 1;

	for (std::string team; std::getline(fwins, team); )
		if (const auto itr = teams.find(team); itr != teams.end())
			++itr->second;
		else
			std::cout << team << " not found\n";

	// Displays all winners if needed
	/*
	for (const auto& [name, wins] : teams)
		if (wins)
			std::cout << name << "  " << wins << '\n';
	*/

	for (std::string nam {" "}; !nam.empty(); ) {
		std::cout << "Enter team name to see if won World Series (CR to exit): ";
		std::getline(std::cin, nam);

		if (!nam.empty())
			if (const auto itr = teams.find(nam); itr != teams.end())
				std::cout << nam << " won " << itr->second << " times\n";
			else
				std::cout << nam << " has not won\n";
	}
}



Enter team name to see if won World Series (CR to exit): New York Mets
New York Mets won 2 times
Enter team name to see if won World Series (CR to exit): qwert
qwert has not won
Enter team name to see if won World Series (CR to exit): Toronto Blue Jays
Toronto Blue Jays won 2 times
Enter team name to see if won World Series (CR to exit): NewYork Yankees
NewYork Yankees has not won
Enter team name to see if won World Series (CR to exit): New York Yankees
New York Yankees won 27 times
Enter team name to see if won World Series (CR to exit):


Note that the team names must all be exactly the same - and no leading/trailing spaces etc.
Here's a version of your code that works. See the comments starting with "dmh"
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
59
60
61
62
63
64
65
66
#include <iostream>
#include <array>
#include <fstream>
using namespace std;

int main()
{
    //variables
    string team, winner;	// dmh: each represents a single team
    // string TeamsList[200] = {};	// dmh: unused
    // string WinnersList[200] = {}; // dmh: unused
    
    int counter = 0;
    
    //open file Teams
    ifstream teamsFile;
    teamsFile.open("Teams.txt");
    
    //If it does not open display "error"
    if(!teamsFile){
        cout << "error" << endl;
        return 0;
    }
    // If it does open display the content of the file and remember it
    cout << "Teams in the league:\n";
    while (getline(teamsFile, team)){
        cout << team << endl;
    }
    //close file
    teamsFile.close();
    
    //ask for a input
    cout << "Enter team name to see if win World Series: \n";
    getline(cin, team);
    
    // dmh: This code doesn't actually do anything
    // bool found = false;
    
    // Search for the input
    // for (int i = 0; i < winners.size(); i++){
    // 	if(TeamsList[i] == teams){
    // 	    found = true;
    // 	    break;
    //  }
    // }    

    //open the winners ifstream
    ifstream winnersFile;
    winnersFile.open("WorldSeriesWinners.txt");
    // If it does not open display "error"
    if(!winnersFile){
        cout << "error" << endl;
        return 0;
    }
    // If it does open count the number of times that the team won
    while(getline(winnersFile, winner)){
        if(winner == team)	// dmh: compare the team input user to the a team in the file.
            counter++;
    }
    //display result
    cout << team << " has won the world series " << counter << " times. \n ";
    
    winnersFile.close();
    
    return 0;
}


Based upon original method, then perhaps:

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
#include <iostream>
#include <fstream>
#include <string>

int main()
{
	std::ifstream teamsFile("Teams.txt");
	std::ifstream winnersFile("WorldSeriesWinners.txt");

	if (!teamsFile || !winnersFile)
		return (std::cout << "Error opening files\n"), 1;

	std::cout << "Teams in the league:\n";

	for (std::string lteam; std::getline(teamsFile, lteam); std::cout << lteam << '\n');

	std::string team;

	std::cout << "\nEnter team name to see if won World Series: ";
	std::getline(std::cin, team);

	size_t counter {};

	for (std::string winner; std::getline(winnersFile, winner); counter += (winner == team));

	std::cout << team << " has won the world series " << counter << " times. \n ";
}

Last edited on
Topic archived. No new replies allowed.