3

I am trying to separate a list of numbers such as: 34,45,12.3,100,34.6,50

I can do it only if there are no decimals like this:

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

int main()
{
    std::string str = "34,45,12.3,100,34.6,50";
    std::vector<int> vect;

    std::stringstream ss(str);

    int i;

    while (ss >> i)
    {
        vect.push_back(i);

        if (ss.peek() == ',')
            ss.ignore();
    }

    for (i=0; i< vect.size(); i++)
        std::cout << vect.at(i)<<std::endl;

}

The problem here is with the decimals. The above will produce:

34 45 12 3 100 34 6 50

while it should produce:

34 45 12.3 100 34.6 50

basically the above code when it sees a dot '.' it acts as if it was a comma.

Any ideas?

Dm. S.
  • 33
  • 1
  • 3
  • 4
    You have a vector of `int` as in integer, thus it cannot hold a floating point number; they get truncated to the integer part. Change your vector so that its elements are floating point numbers. – bku_drytt Apr 21 '16 at 03:02
  • 1
    `i` should be a `float`. – Chnossos Apr 21 '16 at 03:02
  • This is a duplicate of http://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c (but for some reason I don't think marking it as such, and closing it, would be right, here). – Sam Varshavchik Apr 21 '16 at 03:06
  • You should learn the difference between integers and floating point numbers. A hint: Integers don't have fractional parts, so there is no decimal point. – Ken White Apr 21 '16 at 03:08
  • Don't reuse variables! Declare two separate ones. (For instance, you reuse `i` inside your for loop.) – Mateen Ulhaq Apr 21 '16 at 03:17
  • *"The above will produce: `34 45 12 3 100 34 6 50`"* - with which compiler/version, and language locale? I would normally expect it to produce only `34 45 12`, as the `.` in `12.3` should cause the `while (ss >> i)` loop to terminate, and indeed I get only `34 45 12` in testing on VS2013 / MSVC12 on Win7 set to English. – Tony Delroy Apr 21 '16 at 03:53

3 Answers3

3

You should use a float and change the code to use float instead of int:

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

int main()
{
    std::string str = "34,45,12.3,100,34.6,50";
    std::vector<float> vect;

    std::stringstream ss(str);

    float i;

    while (ss >> i)
    {
        vect.push_back(i);

        if (ss.peek() == ',')
        ss.ignore();
    }

    for (i=0; i< vect.size(); i++)
    std::cout << vect.at(i)<<std::endl;

}
randominstanceOfLivingThing
  • 11,543
  • 11
  • 40
  • 66
3

You should declare float i instead of int i and declare the vector as a vector of floats instead of a vector of integers. This is because 12.4 is not an integer, but a float.

anc
  • 171
  • 1
  • 18
rox
  • 435
  • 4
  • 16
1

I think your code interprets 12.3 as two different numbers because you haven't declared that number as a float. You vector holds all of the strings as integers, and not floats. If you declared your vectors as floats instead of integers, then your problem should be solved

Matt.Mull
  • 46
  • 4