1

I have a text file having 3 lines of arbitrary numbers like this:

3.50001
6.70001
812.333

I would want to input and save these 3 numbers into a vector in my programme. It should be read line by line, so i assume the vector size will be 3. But it come wit error that the line in text file is string rather than double. It cannot be saved into the vector. How can I turn these string into double? Thank you.

PS: the function std::stod could not be resolved here properly due to Eclipse.

int main(int argc, char* argv[]) {
#include<vector>
#include <string>
        string line;
        std::vector<double> myvector(3);
          ifstream myfile (argv[0]);
          if (myfile.is_open())
          {
            while ( getline (myfile,line) )
            {
              std::string::size_type sz;
              cout << line << '\n';
              double line_double = std::stod(line);
              myvector.push_back(line_double);

            }
            myfile.close();
Cii
  • 133
  • 8
  • Google yielded [`atof`](http://www.cplusplus.com/reference/cstdlib/atof/). – Elliott Frisch Nov 01 '15 at 05:57
  • 1
    Move `#include` directives to the top of the file. What exact are error messages or unexpected results are you getting? "Doesn't work" is vague. – n. m. could be an AI Nov 01 '15 at 05:59
  • I tried 'atof' but it cannot be resolved due to 'Invalid arguments 'Candidates are: double atof(const char *)' @ElliottFrisch – Cii Nov 01 '15 at 06:02
  • The exact error msg for using "stof" is "Function 'stof' could not be resolved" @n.m. – Cii Nov 01 '15 at 06:04
  • You need to enable C++11 for it to be available. http://stackoverflow.com/questions/17457069/enabling-c11-in-eclipse-juno-kepler-luna-cdt You still need to move your include directives. – n. m. could be an AI Nov 01 '15 at 06:08
  • 1
    Possible duplicate of [Eclipse CDT C++11/C++0x support](http://stackoverflow.com/questions/9131763/eclipse-cdt-c11-c0x-support) – smac89 Nov 01 '15 at 06:24
  • Could also be a CODAN error. Not enough info to give a satisfactory answer. – user4581301 Nov 01 '15 at 06:43
  • *"(...) could not be resolved here properly due to Eclipse."* - Eclipse is just an IDE, a graphical wrapper around the real thing, i.e. the compiler. You should learn to use a compiler manually (on the command line) before dealing with IDEs. – Christian Hackl Nov 01 '15 at 10:40

0 Answers0