1 year ago

#388657

test-img

blindfox916

Prevent a string with from being cast to an int with try/catch

I am using try/catch exception to sort a vector of strings and I want to prevent it from converting a string like "157cm" from being converted to an int. It removes the "cm" part. Is there anyway to prevent this?

My code:

  for (int v = 0; v < values.data.size(); v++) {
        
        try
        {
            int value = std::stod(values.data.at(v));
            values.areints2.push_back(value);
         
        }
        catch (std::exception& e)
        {
            string x;
            x = values.data.at(v);
            values.notints2.push_back(x);
        }
    }

c++

vector

0 Answers

Your Answer

Accepted video resources