looking for advice

hi guys,

I am trying write a simple program which reads from a text file line by line,
and convert each line into an integer and store it into an array.

My textfile is basically a list of numbers. So each line holds a number.

For some reason it is saying that there is no suitable conversion function from std::string to constant char.



#include <iostream>
using std::cerr;
using std::cout;
using std::endl;

#include <fstream>
using std::ifstream;

#include <vector>
#include <string>

using namespace std;

int main() {


vector<int> arr;


ifstream myReadFile;
myReadFile.open("C:\\time\\time.txt");

string line[256];


for(int i = 0; myReadFile.good(); i++)
{

getline( myReadFile, line[i]);
cout<< line[i] <<endl;

int x;

string y = line[i];
x = atof (y) ;

cout<< x <<endl;

}

myReadFile.close();
return 0;
}

atof does not take a std::string:

http://www.cplusplus.com/reference/clibrary/cstdlib/atof/

You need to convert the std::string to a c-string:

http://www.cplusplus.com/reference/string/string/c_str/

i.e. x = atof (y.c_str()) ;


Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
Topic archived. No new replies allowed.