#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
usingnamespace std;
using vec = vector<double>;
using matrix = vector<vec >;
//==========================================================
matrix readCSV( string filename )
{
matrix M;
ifstream in( filename );
string line;
while ( getline( in, line ) ) // read a whole line of the file
{
stringstream ss( line ); // put it in a stringstream (internal stream)
vec row;
string data;
while ( getline( ss, data, ',' ) ) // read (string) items up to a comma
{
row.push_back( stod( data ) ); // use stod() to convert to double; put in row vector
}
if ( row.size() > 0 ) M.push_back( row ); // add non-empty rows to matrix
}
return M;
}
//==========================================================
void write( const matrix &M )
{
constint w = 12;
// cout << fixed;
for ( auto row : M )
{
for ( auto e : row ) cout << setw( w ) << e << ' ';
cout << '\n';
}
}
//==========================================================
int main()
{
matrix M = readCSV( "data.csv" );
write( M );
}
//==========================================================
in my original csv data file, it has got many rows and columns. Could you please help me with a code where I can count the number of rows and columns and then put them in a 2D array?
Could there be an alternative way of writing the code? I upgraded the codeblock compiler to follow C++11, but it still throws an error stating that "stod was not declared in this scope".
Thanks @Thomas1965, I had no way of checking that in an IDE.
@Saptarnee,
As a short-term fix you can create your own function stod()
1 2 3 4 5 6
double stod( string s )
{
double result;
stringstream( s ) >> result;
return result;
}
Put it somewhere above the point of first use.
If it now works then I would recommend renaming it (and the call to it) so that it doesn't conflict with the standard library version when you do eventually get a fully up-to-date compiler.
Firstly thank you so much @lastchance and @Thomas1965. My code compiled after creating a stod() function.
But the thing that concerns me is, I downloaded CodeBlocks version 17.12. Still it throws an error for the stod(). It says stod() is not declared withing this scope.
Is there any way to fix this? I thought C++11 would easily compile in the 17.12 version of CodeBlocks.
Is there any way to fix this? I thought C++11 would easily compile in the 17.12 version of CodeBlocks.
If you have properly set the project setting to use C++11 or higher the problems could be the version of your compiler. If you're using the g++ compiler you need at least version 4.8.1 for full C++11 support (which includes the stoX() functions). I suggest you use an even more modern compiler if possible and using one of the more current standards (C++14 version 6.1, or C++17 version 7.2).
CodeBlocks is an IDE. IDE is not a compiler. IDE does use a compiler. IDE install might include a compiler. The compiler can be replaced.
GCC is a compiler. For many versions, it did use legacy C++ by default. Newer C++ standard support had to be enabled with a command line option. An IDE user does not run compiler from command line. IDE user sets IDE options that the IDE passes to the compiler.
Newer C++ standard support had to be enabled with a command line option.
Actually strict C++ standard support still has to be enabled with a command line option. By default g++ uses -std=gnuXXX which allows several hacks to be used that should be avoided while learning the language, IMO.