dimanche 24 juillet 2022

Struct values going missing when trying to call them?

I'm having quite a bit of issue when it comes to getting my struct values to be used in another function. For some reason it keeps going missing outside of the if loop that it is in.

I saw some posts saying that this happens because it is in an if loop, and will need to do something to pass by reference, but even if I attempt to remove the higher level if statements, I'll still need the if statement to do the regex matching for the values. So I'm really not too sure how to go about doing this and would really appreciate all the help I can get! Thank you!

Some context for this portion of code - I need to store certain values from the file that's loaded in, into different variables within a struct, then call the values after in another function.

Struct & dynamic array declaration:

struct cityLocations
{
    int cityXAxis = 0;
    int cityYAxis = 0;
    int cityID = 0;
    string cityName = "";
};

cityLocations * cityLocationLines;

storeFileData() function:

void storeFileData(int fileID)
{
    string fileline; // for getline
    smatch match; // for regex matching
    ifstream inputFile; // opening stream for inputFile
    int index = 0; // index for the struct array

    // to store the file names
    cityLocationFile = configFileNames[0];

// these two below are basically the same as the citylocation one just with different values
    //cloudCoverFile = configFileNames[1];
    //pressureFile = configFileNames[2];

    
    // to check the values stored: 
    cout << "City location file name: " << cityLocationFile << endl;
    cout << "Cloud cover file name: " << cloudCoverFile << endl;
    cout << "Pressure file name: " << pressureFile << endl;
    cout << endl;
    
    // for citylocation.txt
    if (fileID == 0)
    {
        inputFile.open(cityLocationFile.c_str());
        if (inputFile.is_open())
        {
            while (getline (inputFile, fileline))
            {
                // to count the number of lines within the file so that it can be used to create an array
                if (regex_search(fileline, match, cityLocationPositionRegex))
                {
                    cityLocationCount++;
                }

                //creates an array big enough to store all the lines within the city location file
                cityLocationLines = new cityLocations[cityLocationCount];

                if (regex_search (fileline, match, cityLocationPositionRegex))
                {
                    cityLocationLines[index].cityXAxis = stoi(match[1]);
                    cityLocationLines[index].cityYAxis = stoi(match[2]);
                    cityLocationLines[index].cityID = stoi(match[3]);
                    cityLocationLines[index].cityName = (match[4])

                    index++;
                }

            }
        }
        inputFile.close();
    }

When calling the storeFileData() in another function...

displayMap() function:

void displayMap(int mapID)
{
    // some blocks of code for the border of the map
    // for citylocation.txt
    if (mapID == 0)
    {
        storeFileData(0);   
        
        // /*
        // to test
        // NTS: when this is done, returns weird numBERS
        cout << "Number of cities: " << cityLocationCount << endl;
        for (int index = 0; index<cityLocationCount; index++)
        {
            cout << "X & Y axis: " << cityLocationLines[index].cityXAxis << " & " << cityLocationLines[index].cityYAxis << endl;
            cout << "City ID: " << cityLocationLines[index].cityID << endl;
            cout << "City Name: " << cityLocationLines[index].cityName << endl;
            cout << endl;
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire