Home > Software engineering >  Parsing does not work: terminate called after throwing an instance of "std::invalid argument&qu
Parsing does not work: terminate called after throwing an instance of "std::invalid argument&qu

Time:12-17

I want to have a function which returns a vector of 2 integers. The input is a string.

The layout of the string that is inserted should always be like this: "COORDINATES 123 456" with the coordinates being integers of any length.

If the string is "COORDINATES 123" or "COORDINATES 123 456 789", the function should return an empty vector.

#include <iostream>
#include <string>
#include <vector>

std::vector<int> getCoordinates(std::string string){
    auto count = 0;
    std::string coordinates;
    int coordinatesInt;
    std::vector<int> vector;
    int i, j = 0;
    for(int i = 0; i < string.size(); i  ){
        if(string.at(i) == ' '){
            count  ;
            j = 1;
            while(string.at(i j) != ' ' && string.at(i j) <= string.length()){
                coordinates.push_back(string.at(i j));
                j  ;
            }
            coordinatesInt = std::stoi(coordinates);
            vector.push_back(coordinatesInt);
        }
    }
    if(count != 2){
        vector.clear();
    }
    std::cout << count << std::endl;
    return vector;
}


int main()
{
    std::string coordinates = "COORDINATES 123 456";
    std::vector<int> vectorWithCoordinates = getCoordinates(coordinates);
    std::cout << vectorWithCoordinates[1] << std::endl;
    //vectorWithCoordinates should now contain {123, 456}
    return 0;
}

However, when I run this code, I get an error message saying:

terminate called after throwing an instance of "std::invalid argument"

CodePudding user response:

#include <iostream>
#include <string>
#include <vector>

std::vector<int> getCoordinates(std::string string){
auto count = 0;
std::string coordinates;
int coordinatesInt;
std::vector<int> vector;
for(unsigned i = 0; i < string.size(); i  ){
    if(string.at(i) == ' '){
        count  ;
        unsigned j = 1;
        while(i j<string.size() && string.at(i j) != ' '){ //checks that you do not go out of range before checking the content of the string
            coordinates.push_back(string.at(i j));
            j  ;
        }
        coordinatesInt = std::stoi(coordinates);
        vector.push_back(coordinatesInt);
    }
    coordinates.clear();//clears the string in order to have two different integers
    }
    if(count != 2){
       vector.clear();
    }
    std::cout << count << std::endl;
    return vector;
}


int main()
{
 std::string coordinates = "COORDINATES 123 456";
 std::vector<int> vectorWithCoordinates = getCoordinates(coordinates);
 for(auto i : vectorWithCoordinates)
 std::cout<<i<<"\n";
 //vectorWithCoordinates should now contain {123, 456}
 return 0;
}

The problem in the code was that you tried to access the content of the string at position i j without being sure that that position is not out of range. I made minimal modifications to your code to obtain the right output (I think).

  • Related