The data I want to read is a 1-column csv file storing double values, so in each row there is only one number, no white space, delimiters or anything else.
The code above, however, extracts an empty row after all other rows have been read in, and then crashes due to stod used on an empty row. There is, however, no empty row at the end of my file. What is going on?
Try writing the while statement at line 5 as while (getline(datasource, row)), deleting line 6.
And I really recommend NOT having usingnamespace std; in your code. One day it will bite you in the tuchas and it will be painful. Get used to explicitly using namespace designations.
your recommendation works like a charm. Any idea what went wrong? I would like to put a little warning comment next to the example in the book.
Since I barely use anything but std (and I am lazy), I tend to use usingnamespace std; . But it can't hurt to get used to "good practice" ahead of time I guess as I expect to do more with C++ over the years...
There are a few other books I can recommend that are more narrow in focus, such as the details of what C++20 has to offer, the history and uses of lambdas, the details of C++17, etc.
Just to show how bad books can be a supposedly authorative book I have in my C++ library on an early C++ standard known as ANSI C++ (copyright 1996 by Sams Publishing) has their "Hello World" introduction program as:
The book I use is written by a German CS professor and only has a German edition (just realized the latter). In case you still want to take a look, this is the website to the book: http://www.cppbuch.de/
I use the C++14 version published in 2015. I liked it because it is very beginner friendly and does not expect any programming or CS knowledge whatsoever. It also comes with a large collection of exercises and solutions.
/*
The data I want to read is a 1-column csv file storing double values.
*/
#include <iostream>
#include <fstream>
#include <vector>
usingnamespace std; // Something to draw crabs :)
int main ()
{
double value;
vector<double> vector_of_doubles;
ifstream myfile ("doubles.csv");
if (myfile.is_open())
{
while ( myfile >> value )
{
vector_of_doubles.push_back(value);
cout << value << " is now in the vector\n";
}
cout << '\n';
myfile.close();
}
else
{
cout << "ERROR: Unable to open file";
return -9;
}
// Display vector contents - there are MANY ways to do this
for(int i = 0; i < vector_of_doubles.size(); i++)
{
cout << vector_of_doubles[i] << '\n';
}
cout << '\n';
return 0;
}
Data file called double .csv follows.
Note that the cs in csv stands for comma separated which doesn't mean much of significance here unless each line is terminated with a ',' which would be unorthodox and, as you say, isn't. But who knows for absolute certainty without a sample provided.
1.234
23.7
19
267.089
1.234 is now in the vector
23.7 is now in the vector
19 is now in the vector
267.089 is now in the vector
1.234
23.7
19
267.089
Program ended with exit code: 0
Both are English language, if that matters to you.
The best advice I can give is keep coding, learn from any mistakes you make, try to solve any problem by doing research first doing 'net searches and above all else ask questions.
Show you made an effort to do the work yourself, and most people here will be eager to help.