Jump to content

C++ String to Double Array

BrownZeus

Hey there!

 

Learning C++ on the fly for a school assignment.

I have to read data from a csv and crate objects with it. I have Java experience so I already have my class coded that has the fields from the csv file as fields for the objects.

Now here's where I'm having just a tiny hiccup:

 

The CSV file format is as such

 

val1,val2,val3,{array of doubles},val4

 

I have my code written already that reads the file line by line and reads the data successfully (tested via cout all the fields to the console.)

 

Since the array of doubles get read in as string i need to convert that string into an array of doubles, how do I accomplish this?

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, BrownZeus said:

Hey there!

 

Learning C++ on the fly for a school assignment.

I have to read data from a csv and crate objects with it. I have Java experience so I already have my class coded that has the fields from the csv file as fields for the objects.

Now here's where I'm having just a tiny hiccup:

 

The CSV file format is as such

 

val1,val2,val3,{array of doubles},val4

 

I have my code written already that reads the file line by line and reads the data successfully (tested via cout all the fields to the console.)

 

Since the array of doubles get read in as string i need to convert that string into an array of doubles, how do I accomplish this?

http://www.cplusplus.com/reference/cstdlib/strtod/

Link to comment
Share on other sites

Link to post
Share on other sites

39 minutes ago, BrownZeus said:

Helpful but not 100%. This is for single values. I need to convert a whole strong into an array.

 

"{2.5, 3.5, 36.4} into a double array

#include <iostream>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main()
{
    cout << "Goodbye world!" << endl;

    const int _size = 10;
    double _array[_size];

    char _string[] = "1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 666.666";
    char *_sz;

    for(int i = 0; i < _size; i++)
    {
        _array[i] = strtod(_string, &_sz);
        strcpy(_string, _sz);
        cout << i << ": " << _array[i] << endl;
    }
    return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, BrownZeus said:

"{2.5, 3.5, 36.4} into a double array

Into a vector I'll assume, as array sizes are fixed at compile time.

 

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

int
main()
{
    std::string exampleString = "{2.5, 3.5, 36.4}";

    //Test if string begins with { and ends with }.
    if (exampleString.front() != '{' || exampleString.back() != '}')
    {
        std::cout << "String does not begin with '{' and/or end with '}'\n";
        return 1;
    }

    //Replace {} and comma's with whitespace.
    exampleString.front() = exampleString.back() = ' ';
    std::replace(exampleString.begin(), exampleString.end(), ',', ' ');

    //Create stringstream from string.
    auto iss = std::istringstream(exampleString);

    //Create vector of doubles from stringstream.
    const auto doublesVector = std::vector<double>(std::istream_iterator<double>(iss),
                                                   std::istream_iterator<double>());

    //Check for errors.
    if (!iss && !iss.eof())
    {
        std::cout << "Error parsing string.\n";
        return 2;
    }

    //Print result.
    for (const auto d : doublesVector)
    {
        std::cout << d << ' ';
    }

    return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×