0

I am struggling to understand what I need to do to access arrays that were created within the functions I have written. I need to be able to access it so I can continue to the next part of the assignment I'm working on. In the code, I can list out the teams, enter the team name and get info on how many times they have won, but I can't access the arrays created from the text files to create a new function that then compares the arrays. Code is below. Any ideas?

#include <fstream>
#include <string>
using namespace std;

//Function prototypes
void wonOnceList(string[], int&);
void teamWinsList(string, string[], int&);

int main()
{

    string teamNameEntered;
    const int ARRAY_SIZE = 100; // Array size
    string teamName[ARRAY_SIZE]; // Array with team names that have won at least once
    string worldSeriesWinnersList[ARRAY_SIZE]; //list of winning teams from 1903 to 2012

    wonOnceList(teamName[], ARRAY_SIZE);

    cout << "\nEnter the name of one of the teams listed above to find out the number\n";
    cout << "of times that team has won the World Series from 1903 to 2012." << endl;
    getline(cin, teamNameEntered);
    cout << "You entered " << teamNameEntered << endl;

    teamWinsList(teamNameEntered);

    return 0;
}

void wonOnceList(string teamName, int& count) {
    const int ARRAY_SIZE = 100; // Array size
    string teamName[ARRAY_SIZE]; // Array with 100 elements
    int count = 0; // Loop counter variable
    ifstream inputFile; // Input file stream object

    inputFile.open("Teams.txt"); // Open the file.

// Read the team names from the file into the array.
    while (count < ARRAY_SIZE && getline(inputFile, teamName[count]))
        count++;

    // Close the file.
    inputFile.close();

    // Display the teams read.
    cout << "The teams that have won the World Series: \n";
    for (int index = 0; index < count; index++)
        cout << teamName[index] << endl;

}


void teamWinsList(string winners, string worldSeriesWinnersList[], int& ARRAY_SIZE) {
    int timesWon = 0; //wins counter
    const int ARRAY_SIZE = 100; // Array size
    string worldSeriesWinnersList[ARRAY_SIZE]; // Array with 100 elements
    int count = 0; // Loop counter variable
    ifstream inputFile; // Input file stream object

    inputFile.open("WorldSeriesWinners.txt"); // Open the file.

// Read the team names from the file into the array.
    while (count < ARRAY_SIZE && getline(inputFile, worldSeriesWinnersList[count]))
        count++;

    // Close the file.
    inputFile.close();

    //loop determining wins per team
    for (int i = 0; i < ARRAY_SIZE; i++) {
        if (winners == worldSeriesWinnersList[i])
            timesWon++;
    }

    //outputing times won per team
    cout << "\nThe team " << winners << " has won the World Series ";
    cout << timesWon << " times from 1903 to 2012." << endl;
}```
  • 1
    If your array is a local variable of a function, its lifetime ends when execution of the function concludes. – einpoklum Feb 28 '21 at 19:21
  • Do the answers [here](https://stackoverflow.com/q/6441218/1593077) address your question? – einpoklum Feb 28 '21 at 19:23
  • 1
    You need to learn about "scope" - all names - including that of variables - have a "scope". Use that as a [search term](https://duckduckgo.com/?q=c%2B%2B+variable+scope&atb=v233-1&ia=web) and you'll find all kinds of useful and _necessary_ information to help you get started with C++. (Also: any other computer language. Nearly all have ideas of _scope_ - just the rules are different from language to language.) – davidbak Feb 28 '21 at 19:23

0 Answers0