Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im having trouble with my code. I need it to show me how many times the selected

ID: 3683182 • Letter: I

Question

Im having trouble with my code. I need it to show me how many times the selected team has won but ever time i run it it says the selected team won 0 times. Not sure what I'm missing here.

#include<iostream>

#include<iomanip>

#include<fstream>

#include<string>

#define YEARS 107

using namespace std;

int main()

{

  

ifstream inFile, inFile2;

string team_name, chosen_team,winner_team;

int win_times=0;

inFile.open("Teams.txt");

inFile2.open("WorldSeriesWinners.txt");

if (inFile && inFile2)

{

  

  

while (!inFile.eof())

{

getline(inFile,team_name);

cout<<team_name;

}

cout<<"Please select the team ";

getline(cin,chosen_team);

while (!inFile2.eof())

{

   getline(inFile2,winner_team);

  

   if(chosen_team==winner_team)

{

   win_times++;

}

  

}

cout<<chosen_team<<" won "<<win_times<<" times."<<endl;

  

}

  

else

{

cout<<" Error! File not opened. ";

}

  

inFile.close();

inFile2.close();

return 0;

  

}

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#define YEARS 107
using namespace std;

int main()
{

   ifstream inFile, inFile2;
   string team_name, chosen_team, winner_team;
   int win_times = 0;
   inFile.open("Teams.txt");
   inFile2.open("WorldSeriesWinners.txt");
   if (inFile && inFile2)
   {


       while (!inFile.eof())
       {
           getline(inFile, team_name);
           cout << team_name;
       }
       cout << "Please select the team ";
       getline(cin, chosen_team);
       while (!inFile2.eof())
       {
           getline(inFile2, winner_team);


           if(chosen_team.compare(winner_team) == 0) // strings in c++ should be compared like this
           {
               win_times++;
           }

       }
       cout << chosen_team << " won " << win_times << " times." << endl;

   }

   else
   {
       cout << " Error! File not opened. ";
   }

   inFile.close();
   inFile2.close();
   return 0;

}