Extracting specific data without the rest in a .txt file

.txt file:
Johnson 60 12.50
Aniston 65 13.25
Cooper 50 14.50
Gupta 70 14.75
Blair 55 10.50
Clark 40 18.75
Kennedy 45 20.50
Bronson 60 20.00
Sunny 65 18.75
Smith 30 9.75

So basically what i'm trying to do here is take the second column of numbers and only that, so for instance looking at line 1 of the .txt file I'm attempting to only extract '60' which is the number of hours worked. On my code i get the correct value for 'hr1' however when I continue this method onto hr2 it doesn't work and it prints 12. So my question is what do I need to do in order to jump to the next line so hr2 is '65'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  int main()
{
    int hr1, hr2;
    ifstream in_data;

    in_data.open("ch8_Ex17Data.txt");

    in_data.ignore(50, ' ');
    in_data >> hr1;
    in_data.ignore(50, ' ');
    in_data >> hr2;

    in_data.close();

    cout << hr1 << endl;

    return 0;
}
I think instead of int, float/double should be used for hr2 since hr2 since the value that will be stored in hr2 is a decimal (int is only for integers).

I tried the following code:

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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int hr1;
    double hr2;
    ifstream in_data;

    in_data.open("ch8_Ex17Data.txt");

    in_data.ignore(50, ' ');
    in_data >> hr1;
    in_data.ignore(50, ' ');
    in_data >> hr2;

    in_data.close();

    cout << hr1 << endl;
    cout << hr2 << endl;

    return 0;
}


(I also made the ch8_Ex17Data.txt file to be opened) and the result I got after compiling was:

1
2
60
12.5


About jumping to line 2, I haven't learned much C++ yet so I don't know :(
Your second number is a double not an int. You don't have to ignore() etc, it's much simpler.

It's worth checking the file exists too. See: http://www.cplusplus.com/doc/tutorial/files/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string dummy;
    int hr1{0};
    double no_use_to_me{0};
    fstream in_data;
    
    in_data.open("ch8_Ex17Data.txt");
    
    while(in_data >> dummy >> hr1 >> no_use_to_me)
    {
        cout << hr1 << '\n';
    }
    
    in_data.close();
    return 0;
}



60
65
50
70
55
40
45
60
65
30
Program ended with exit code: 0
Registered users can post here. Sign in or register to post.