I have a csv file with an 5 columns and 100 rows. My objective is to load the file into a vectorData
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int count = 0;
vector<double> data;
string line;
cout << "Testing loading of file." << endl;
ifstream myfile ("iris.csv");
if ( myfile.is_open() )
{
while ( ! myfile.eof() )
{
getline (myfile, line);
data.push_back(line);
// logs.at(count) = line;
count++;
}
myfile.close();
}else{
cout << "Unable to open file." << endl;
}
cout << "the log count is: " << count << endl;
return 0;
}
I tried writing the above code to just enter 1 value into the vector but when I try to compile i get errors
lab6.cpp: In function ‘int main()’:
lab6.cpp:22:35: error: no matching function for call to ‘std::vector<double>::push_back(std::__cxx11::string&)’
data.push_back(line);
^
In file included from /usr/include/c++/6.3.1/vector:64:0,
from lab6.cpp:4:
/usr/include/c++/6.3.1/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = double; _Alloc = std::allocator<double>; std::vector<_Tp, _Alloc>::value_type = double]
push_back(const value_type& __x)
^~~~~~~~~
/usr/include/c++/6.3.1/bits/stl_vector.h:914:7: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const value_type& {aka const double&}’
/usr/include/c++/6.3.1/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = double; _Alloc = std::allocator<double>; std::vector<_Tp, _Alloc>::value_type = double]
push_back(value_type&& __x)
^~~~~~~~~
/usr/include/c++/6.3.1/bits/stl_vector.h:932:7: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘std::vector<double>::value_type&& {aka double&&}’
Can someone point me in the right direction Of how i can modify the code or start from scratch in order to load values into a 2d Vector?
sample data from csv file.
-0.57815,0.83762,-1.0079,-1.0369,-1
-0.88983,-0.20679,-1.0079,-1.0369,-1
-1.2015,0.21097,-1.0769,-1.0369,-1
-1.3573,0.0020888,-0.93891,-1.0369,-1
-0.73399,1.0465,-1.0079,-1.0369,-1
-0.11064,1.6731,-0.80094,-0.683,-1
-1.3573,0.62874,-1.0079,-0.85994,-1