mardi 19 juillet 2022

Program will exit after running a specific function in C++. Unable to figure out why

I have this function that will split the string. After running it in a while loop of read file, the program will exit instead of continue the next step of functions.

vector <string> splitString (string str, string delim){
vector <string> result; 
size_t pos = 0; 
string token;

while ((pos = str.find(delim)) != std::string::npos){
    token = str.substr(0, pos);
    result.push_back(token);
    str.erase(0, pos+delim.length());
}

if (!str.empty())
    result.push_back(str);

return (result);
}

void readCityLocation(int ** cityMap){
ifstream inFile;
inFile.open(filename[0]);
string line;
while (getline(inFile, line)){
    int xCoordinate;
    int yCoordinate;

    string xValue;
    string yValue;
    
    //Get xCoordinate...
    vector <string> splitThis = splitString(line,",");

    xValue = splitThis[0];

    cout << line;

}

An function called option2(), will create a table and call void readCityLocation( )

void option2(){
cout << endl << endl;
int ** table = new int * [gridXEnd + 1];
for (int i = 0; i < gridXEnd + 1; i++) {
    table[i] = new int[gridYEnd + 1];
}

//Initialize all the array value in table to be 0
for (int i = 0; i < gridXEnd + 1; i++) {
    for (int j = 0; j < gridYEnd + 1; j++) {
        table[i][j] = 0;

        //To be remove: Error Handling
        //cout << table[i][j] <<  " Grid X: " << i << " Grid Y: " << j << endl;
    }
}

readCityLocation(table);
}

I am fairly new to all this and can't figure out whats the problem. Any assistance is appreciated. Thank you.

Aucun commentaire:

Enregistrer un commentaire