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:
#include <iostream>
#include <array>
#include <fstream>
usingnamespace 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;
}
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.
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
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
#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 (constauto 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 (constauto 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.
#include <iostream>
#include <array>
#include <fstream>
usingnamespace 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;
}
#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 ";
}