lundi 1 mai 2017

QT C++ stof function doesn't convert float string value to normal float

Write to you because I have enough of one error, that could be because of compiler, QT or anything... On project I have loading float data from file:

for (std::string line; getline(file, line);)
{
    size_t pos = line.find_first_of(',');
    if (pos == std::string::npos)
    {
      continue;
    }

    std::string className = line.substr(0, pos);

    size_t classNamePos = className.find_first_of(' ');
    if (classNamePos != std::string::npos)
        className = className.substr(0, classNamePos);

    std::string features = line.substr(pos+1);

    std::vector<float> featuresValues;

    while (true)
    {

        pos = features.find_first_of(',');
        if (pos != std::string::npos)
        {
            std::string feature = features.substr(0, pos);
            std::cout<<"Feature value string: "<<feature<<std::endl;
            features = features.substr(pos + 1);
            float featureValue = std::stof(feature);
            featuresValues.push_back(featureValue);
            std::cout<<"Feature value: "<<std::fixed<<std::setprecision(5)<<featureValue<<std::endl;
        }
        else
        {          

         float featureValue = std::stof(features);
         std::cout<<"Feature value string: "<<features<<std::endl;
                featuresValues.push_back(featureValue);
                std::cout<<"Feature value: "<<std::fixed<<std::setprecision(5)<<featureValue<<std::endl;
                break;
        }
    }

What is interesting on line:

std::cout<<"Feature value string: "<<feature<<std::endl;

Everything is ok, and I get float value:

Feature value string: 0.017578

But in moment of converting to float:

float featureValue = std::stof(feature);
std::cout<<"Feature value: "<<std::fixed<<std::setprecision(5)<<featureValue<<std::endl;

everything's messed up:

Feature value: 0.00000

What can be a problem or what I'm doing wrong and how can I resolve this?

Aucun commentaire:

Enregistrer un commentaire