Loop problem in simple code


I decided to try C++ again after LUA got me nowhere, and I need a little help with some small 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
26
27
28
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    
    string Line;
    ifstream Source ("myname.txt");
    
    if ( Source.is_open() ) {
         
         while ( Source.good() ) {
               
               Source.seekg (11, ios::beg);
               getline(Source,Line);
               cout << Line << endl;
         
         }
       
    }
    
    Source.close();
    system("pause");
    
    return 0;
    
}
    


The propose of this program is to read and text file, and hopefully only print the names to console. Unfortunately seekg is causing the console to loop "Bill" and without seekg it will read the entire text file just fine.

my name is bill
my name is earl
etc...

it loops Bill.

This is not a homework problem, I just remeber seeing someone do this before and thought it would be a good starting point. I have checked several sites, and sadly none of my friends know C++ either, any help would be appreciated.
Last edited on
Yes I have, in fact that is the page where I am getting my info. The examples given, however, do not include a while loop.
Last edited on
I think I found the problem, seekg is putting the cursor 11 away from the beginning of the file, not the beginning of the line.
Topic archived. No new replies allowed.