File input

I'm having some trouble with file input.
friends of jimmy:
alice 301.2

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:
1
2
3
4
string friend1,friend2;
double distance;

cout << friend1 << " is friends with " <<friend2<<" and lives " << distance << "miles away." <<endl; 
Last edited on
closed account (28poGNh0)
I modify your text file to be like this

jimmy is friend with
alice 301.2
bill 9.42
karen 12.4

arnold is friend with
toby 454.123
cameron 943.1
mary beth 48.1
nancy 834


I hope you dont mind and I hope you enjoy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# include <iostream>
# include <fstream>
using namespace std;

int main()
{
    ifstream in("c:\\file.txt");
    if(in)
    {
        for(int i=0;i<2;i++)
        {
            string str,str2;
            in >> str;
            str2 += (str+" ");
            in >> str;
            str2 += (str+" ");
            in >> str;
            str2 += (str+" ");
            in >> str;
            str2 += (str+" ");

            for(int j=0;j<3;j++)
            {
                cout << str2;
                in >> str;
                cout << str << " and lives ";
                in >> str;
                cout << str << " miles away " << endl;
            }
        }
    }else cout << "Cannot find/open this file";

    in.close();

    return 0;
}
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.
closed account (28poGNh0)
I hope thats the one

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# include <iostream>
# include <fstream>
using namespace std;

int main()
{
    ifstream in("c:\\file.txt");
    if(in)
    {
        for(int i=0;i<2;i++)
        {
            string _friend,_friend2;
            in >> _friend;
            in >> _friend;
            in >> _friend;
            in >> _friend2;

            double distance = 0;

            for(int j=0;j<i+3;j++)
            {
                in >> _friend2;
                in >> distance;

                cout << _friend << " is friend with " << _friend2 <<
                " and lives " << distance << " miles away." <<endl;
            }cout << endl;
        }
    }else cout << "Cannot find/open this file";

    in.close();

    return 0;
}
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include <string>
using namespace std;

int main() 
{
    std::string line;
    std::string theFriend ;
    std::string distance ;

    const char* digits = "01234567890." ;


    // std::getline(std::cin, line) ;
    line = "mary beth 48.1" ;  // for illustration's sake.

    std::size_t pos = line.find_first_of(digits) ;
    if ( pos == std::string::npos )
    {
        // didn't find anything.
    }
    else
    {
        theFriend = line.substr(0, pos-1) ;
        distance = line.substr(pos) ;
    }

    std::cout << theFriend << ", " << distance << '\n' ;
}    



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.
Topic archived. No new replies allowed.