error with std::ostringsteam and std::string

Posted by pyCthon on Stack Overflow See other posts from Stack Overflow or by pyCthon
Published on 2012-09-24T02:55:59Z Indexed on 2012/09/24 3:37 UTC
Read the original article Hit count: 313

Filed under:

Hi i want to save many different csv files from a function with a naming convention based on a different double value. I do this with a for loop and pass a string value to save each .csv file differently. Below is an example of what I'm trying to do the desired result would be

1.1_file.csv
1.2_file.csv

but instead i get

1.1_file.csv
1.11.2_file.csv

Here is a working sample code, what can i do to fix this

#include <sstream>
#include <iomanip>
#include <cmath>
#include <iostream>
#include <vector>

int main(){
    std::string file = "_file.csv";
    std::string s;
    std::ostringstream os;
    double x;

    for(int i = 0; i < 10; i++){
        x = 0.1 + 0.1 *i;
        os << std::fixed << std::setprecision(1);
        os << x;
        s = os.str();
        std::cout<<s+file<<std::endl;
        s.clear();
    }

    return 0;
}

© Stack Overflow or respective owner

Related posts about c++