Using loop to read in from a file.

I'm trying to write a code that reads in information from a file with a while loop, but whenever the loop starts over it seems to start at the beginning of the file again. I'm trying to use seekg/tellg but it still doesn't seem to work, it's just an endless loop that start at the beginning of the file again every time. Below is the while loop I'm having issues with (inside main) and the two functions that are in the while loop (outside main). Any help would be appreciated!

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
  car = GetCar(dataIn);

  while(dataIn){
    dataIn.seekg(position+1);
    car.price = car.price * 1.10;
    cout << car.customer << " has this car been resold? [Y/N] ";
    cin >> answer;
    if((answer == 'y') || (answer == 'Y')){
      CarSold(date, car);
      WriteCar(dataSold, car);
    }else{
      WriteCar(dataOut, car);
    }
    GetCar(dataIn);
    position = dataIn.tellg();
  }


Car GetCar(ifstream& dataIn){
  Car car;
  car.sold = false;
  dataIn >> car.customer;
  dataIn >> car.price >> car.purchased.year
         >> car.purchased.month >> car.purchased.year;
  dataIn.ignore(2, '\n');
  return car;
}

void WriteCar(ofstream& dataOut, Car car){
  dataOut << "Customer: " << car.customer << endl
          << "Price:    " << car.price << endl
          << "Purchased:" << car.purchased.day << "/"
          << car.purchased.month << "/"
          << car.purchased.year << endl;
}


If you need any other code to understand the issue then just let me know.
Well, I would say that line 14 should be car = GetCar(dataIn);. However, I can't see anything particularly wrong with your use of seekg & tellg (though it looks to me that their use is pretty much redundant here?), apart from that I don't think you need the +1 in your seekg function call.
Topic archived. No new replies allowed.