-1

My string looks this:

89.800000
89.800000
91.840000
87.760000
60.500000

And I need to split it to double array.

double* data = new double[20];

I need to read string line by line and convert it to double array. Please help. Thank you.

Why its out of range? Please help. Here if (data.at(i) < minimum)

string vyhodnoceni(string nazev_souboru, double &minimum) { // vyhodnocuje volné místo na disku
    string alarm;
    string s = vypocet2(nazev_souboru);
    string st = s + "\n";
    //cout << st;
    bool dataok = true;
    bool bad = false;

    vector<string>::iterator a;
    istringstream sin(st);
    vector<double> data;
    double next = 0.0;
    while (sin >> next) {
        data.push_back(next);
    }
    process(data);

    for (size_t i = 0; i <= data.max_size(); i++)
    {
        if (data.at(i) < minimum)
        {
            dataok = false;
        }
        else
        {

        }

        if (data.at(i) == 0)
        {
            bad = true;
        }
    }

    if ((vypocet(nazev_souboru) < minimum) || (dataok == false)) {
        alarm = "LDS";
    }
    else alarm = "OK";

    if ((vypocet(nazev_souboru) == 0) || (bad == true)){
        alarm = "UER";
    }

    return alarm;
}
  • 3
    C++ has built-ins for that, such as this one: http://www.cplusplus.com/reference/string/stod/ – Blaze Oct 16 '18 at 06:56
  • 1
    stod make double from 89.8000000 but i need to split lines. – user10511250 Oct 16 '18 at 06:58
  • 1
    If your string contains multiple lines or numbers, and you want to copy all numbers in the string to e.g. a vector, then I suggest you read about [`std::istringstream`](https://en.cppreference.com/w/cpp/io/basic_istringstream), [`std::istream_iterator`](https://en.cppreference.com/w/cpp/iterator/istream_iterator), and of course about [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) (and its [constructor](https://en.cppreference.com/w/cpp/container/vector/vector)). All you need is only two lines of code (one to define the `istringstream` object, and one for the vector). – Some programmer dude Oct 16 '18 at 06:59
  • 1
    Instead of `stod`, I would suggest [`to_string`](https://en.cppreference.com/w/cpp/string/basic_string/to_string) (cplusplus.com is hilarious: *'This page describes a feature introduced by the latest revision of the C++ standard (2011). Older compilers may not support it.'*) – hellow Oct 16 '18 at 07:06
  • 1
    Maybe useful https://stackoverflow.com/search?q=%5Bc%2B%2B%5D+read+file+line+by+line – Galik Oct 16 '18 at 08:00
  • 1
    In for (size_t i = 0; i <= data.size(); i++), <= should be <. – Yang Oct 16 '18 at 16:49
  • 1
    Thank you for help – user10511250 Dec 04 '18 at 11:26
  • 1
    Is there any next better solution? – user10511250 Dec 04 '18 at 11:26

3 Answers3

4

If your input is from a string:

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

void process(const std::vector<double>& data) {
  for (const double x : data) {
    std::cout << x << std::endl;
  }
}

int main() {
  std::string input = "89.800000\n 91.840000";
  std::istringstream sin(input);
  std::vector<double> data;
  double next = 0.0;
  while (sin >> next) {
    data.push_back(next);
  }
  if (!sin.eof()) {
    std::cerr << "Warning: encountered non-double value." << std::endl;
  }
  process(data);
  return 0;
}

If your input is from a file "input.txt":

#include <fstream>
#include <vector>

int main() {
  std::ifstream fin("input.txt");
  std::vector<double> data;
  double next = 0.0;
  while (fin >> next) {
    data.push_back(next);
  }
  if (!fin.eof()) {
    std::cerr << "Warning: encountered non-double value." << std::endl;
  }
  process(data);
  return 0;
}
Yang
  • 7,712
  • 9
  • 48
  • 65
0
                istringstream sin(st);
                vector<double> data;
                double next = 0.0;
                bool tecmez = false;
                vector<string> vstr;

                while (sin >> next) {
                    ostringstream strs;
                    strs << next;
                    string str = strs.str();
    data.push_back(next);
}
Adam
  • 151
  • 8
0

First of all you'll really want to use a vector rather than an array... Which I see you're already doing, but you can do it a little cleaner using an istream_iterator:

vector<double> data { istream_iterator<double>(sin), istream_iterator<double> {} }

As far as evaluating out of range, you can easily evaluate if there are any elements less than minimum using any_of and either a lambda or a bind statement, for example:

any_of(cbegin(data), cend(data), bind(less<double>(), placeholder::_1, minimum))

Live Example

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288