Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Giving me the wrong answer

0 votes
asked Apr 21, 2021 by Norma Tacuri (120 points)

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>

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;

}

1 Answer

+1 vote
answered Apr 27, 2021 by Peter Minarik (86,180 points)

I've highlighted lines with problems in yellow. I also added comment after the line to show what the problem is.

#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++) // winners is not set, it's always an empty string
    {
        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) // This is always true. A variable always equals itself
            counter++;
    }

    //display result
    cout << teams << " has won the world series " << counter << " times. \n ";
    winnersFile.close();
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...