mardi 31 mars 2020

C++ Precision on stol and stoi for user input

Trying to parse a float out of a String, it returns with a precision of 0 , I'm looking for 0.0 .

Code:

queue<Car> getInputData() {
    string input;
    string line;
    string delin = " ";
    string::size_type sz = 0.0; 
    int counter = 0;
    queue<Car> cars;
    while (getline(std::cin, line) && !line.empty()) 
        {
        if (counter != 0)
            {
            queue<string> parse = getStringList(line, " ");
            _directions dir;
            int cid = std::stoi(parse.front());
            parse.pop();
            long arriv = std::stoi(parse.front()); //This returns 1 rather than 1.1
            parse.pop();
            dir.start = parseToDirection(parse.front(), true);
            parse.pop();
            dir.end = parseToDirection(parse.front(), false);
            parse.pop();
            Car car = Car(dir, cid, arriv);
            cars.push(car);

        }
        counter++;
    }
return cars; 
}

Sample Input:

cid arrival_time dir_original dir_target

0 1.1 ^ ^

1 2.0 ^ ^

2 3.3 ^ <

3 3.5 v v

4 4.2 v >

5 4.4 ^ ^

6 5.7 > ^

7 5.9 < ^

What I've tried: I've tried stol, stoi, stold, stod and none of them work for precision. Is this possible? Or is there a work around that I'm missing?

Aucun commentaire:

Enregistrer un commentaire