I'm having some trouble with file input.
friends of jimmy:
I have the above file and would like to output it like this:
jimmy is friends with alice and lives 301.2 miles away.
I would like to extract the double as well and store it in a double variable.
I've tried using getline() and the ignore() function but I can't get it quite right. Any help would be appreciated.
Something like this:
cout << friend1 << " is friends with " <<friend2<<" and lives " << distance << "miles away." <<endl;
Last edited on
I'm sorry I wanted to be able to extract the double as well. Sorry for not being clear. I edited the original post, hopefully its a bit more clear as to what I'm trying to do.
Thank you its close to what I want. In my file I have a name "mary beth" and the code you provided only stores "mary" and ignores the rest. Any way to fix this?
Cire your solution seems great, how could i convert that distance string to a double?
If your compiler supports it (C++11), you can use
double value = std::stod(distance) ;
If it doesn't, you can use
double value = std::strtod(distance.c_str(),0);.
#include <cstdlib> would be required in that case.