// WOLD SERIES FROM 1903 TO 2012 // THIS IS MY OUTPUT LOOK LIKE /*Please enter y
ID: 3735066 • Letter: #
Question
// WOLD SERIES FROM 1903 TO 2012
// THIS IS MY OUTPUT LOOK LIKE
/*Please enter your team name!
Minnesota Twins
The Minnesota Twins have won the World Series 0 time(s).
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main ()
{
string team, line;
int count = 0;
cout << "MLB teams that have won the World Series at least once ";
cout << "====================================================== " << endl;
vector<string> teams = { " " };
vector<string> teamswin = { " " };
// Openning file names Teams.txt and WorldSeriesWinners.txt
ifstream readFile ("Teams.txt");
ifstream readFile2 ("WorldSeriesWinners.txt");
while(getline (readFile,line))
{
cout << line << endl;
}
// Error openning file names Team.txt, WorldSeriesWinners.txt , or both files
if (readFile.fail() && readFile2.fail())
{
cerr << "Error openning file " << endl;
exit(1);
}
// Ask the user to enter team name
cout << " Please enter your team name! " << endl;
getline(cin,team);
while(getline (readFile2,line))
{
teamswin.push_back(line);
}
for (int n = 0; n < teamswin.size(); n++)
{
if (team == teamswin[n])
{
count++;
}
}
cout<< "The " << team << " have won the World Series " << count << " time(s)." << endl;
system("PAUSE");
readFile.close();
readFile2.close();
return 0;
}
Explanation / Answer
Your code is perfect except that it has some small mistakes -
1) While you are reading Teams.txt file you should push it back to a vector as to check whether the user input is a team or not.
2) If condition that you put for the error while opening the file is wrong, you've used an and condition whereas it should OR as if any one file fails to oprn you'll need to terminate to execution of the program and notify the user.
Rest of the code is perfect.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.