Basically, I know virtually nothing about C++ and have only programmed briefly in Visual Basic.
I want a bunch of numbers from a csv file to be stored as a float array. Here is some code:
string stropenprice[702];
float openprice[702];
int x=0;
ifstream myfile ("open.csv");
if (myfile.is_open())
{
while ( myfile.good() )
{
x=x+1;
getline (myfile,stropenprice[x]);
openprice[x] = atof(stropenprice[x]);
...
}
...
}
Anyways it says:
error C2664: ‘atof’ : cannot convert parameter 1 from ‘std::string’ to ‘const char *’
Well, you’d have to say
atof(stropenprice[x].c_str()), becauseatof()only operates on C-style strings, notstd::stringobjects, but that’s not enough. You still have to tokenize the line into comma-separated pieces.find()andsubstr()may be a good start (e.g. see here), though perhaps a more general tokenization function would be more elegant.Here’s a tokenizer function that I stole from somewhere so long ago I can’t remember, so apologies for the plagiarism:
Usage:
std::vector<std::string> v = tokenize(line, ",");Now usestd::atof()(orstd::strtod()) on each string in the vector.Here’s a suggestion, just to give you some idea how one typically writes such code in C++: