What usually be the cuase of the file reading the wrong data, lets say I have a file and I wana read a date which is the 1st data in the file but instead is reading something else

My file is more like the following but instead of reading the date is reading surname
I have this statement, InFile >> date but its reading and writting surname

date Name Surname
13-02-2004 Ellethy Pytro

Here is the code

int x;
	int seats = 0;
	//open files
	ofstream TempFile;
	TempFile.open("TempFile.dat");
	ifstream capeFile;
	ifstream durbsFile;
	capeFile.open("Cape2Durban.dat");
	durbsFile.open("Durban2Cape.dat");
	//Accept a date and flight number
	cout << "Enter Date: ";
	cin >> date;
	cout << "Flight Num: ";
	cin >> flight;

	for(int count = 0; count < Size && capeFile >> F.date; count++)
	{
		//B1[count].setFlightDetail(F);
		capeFile >> F.FlightNum;
	}
	cout << F.date << "   " << F.FlightNum;
	cout << endl;
	for(int count = 0; count < Size && durbsFile >> F.date; count++)
	{
		//B1[count].setFlightDetail(F);
		durbsFile >> F.FlightNum;
	}
	cout << F.date << "   " << F.FlightNum;//outputin to see if reading date and flight
	cout << endl;

	//If a match is found
	/*for(int x = 0; x < Size; x++)
	{
		B1[x].isFlightAvailable(date);
	}*/
	if(date == F.date && flight == F.FlightNum)//its not working simple bcoz its not reading  the dates
	{
		//Accept the passenger detail
	}
	else 
		cout << "No match found" << endl;

	//If No match is found
		//Display error message

If the file is in the format 13-02-2004 Ellethy Pytro then your program is reading it incorrectly. It is only reading two fields, not three.

for(int count = 0; count < Size && capeFile >> F.date; count++)
	{
		capeFile >> F.FlightNum;
	}

The above code works for input like:

13-02-2004 123
13-02-2004 123456
13-02-2004 123456789

Maybe your input is of different format?

The code you posted doesn't make a lick of since when compared to what you said in your original post. The code you posted is reading date and FlightNumber, not date name and Surname (three fields)

I have to read two fields which is date and flight and compare them with the ones that user entered if found the program has to accept the user's details which are names and so forth so the prob is the file is reading time instead of date and flight, naver about the above file I was just trying to explain what i was saying I didnt think it nessecary to post my files here but if u need them I would post them definatelh

Either learn to use your compiler's debugger so that you can see the values of the variables as the lines are executed, or add more print statements. For example move line 21 up inside the loop.

Im sorry about that too, I also thought my program would do the same too such as reading the date and flight number

the format of my file is of the ff
date FlightNum DepartAt ArriveAt AirGraft Price Seats
13-06-2001 FL451 10:00 11:00 414 200 451

So what my prog does is reading the DepartAt and Arrive data instead of date and Flight Num

Ok I got it now, it reads and write everything from the file, What i dont understand how come coz I have read only two variables and output only them, so there is just no way it could find the date and the flight number coz it starts reading from the departAt and arriveAt then goes to the second line thats where it starts from the date, Anyway if u have no idea what could be wrong dont kill ur butt about it ok

The program has to read all the fields, not just two of them, inside the loop.

for(int count = 0; count < Size && capeFile >> F.date; count++)
{
          capeFile >> F.FlightNum >> DepartAt >> ArriveAt
              >> AirGraft >> Price >> Seats;
}

Again!!!!!!!!!!!! even if you need two!!!! wow then C++ is getting closer to what we call "more than complicated" where am coming from

Ok now what could be the problem on this statement if(date == F.date && flight == F.FlightNum)

The if statement is not working at all and when outside the loop it only reads the last date and flight number from the file, ok now how do I take my if statement inside the loop without having to ask the user to enter their details until eof for 30 times if the size of the file is 30

The if statement is not working at all and when outside the loop it only reads the last date and flight number from the file

No it hasn't -- the loop reads every line in the file. The if statement after the loop sees only the last line in the file, or the last variables that were read. All other data in the file are just tossed into the bit bucket because there is nothing in the loop to capture them.

ok now how do I take my if statement inside the loop without having to ask the user to enter their details until eof for 30 times if the size of the file is 30

The if statement on line 36 has nothing at all to do with asking anybody anything.

I suppose its a logic error so I wouldnt know, what I know is... that if statement is not working at all, I have tryied to move it inside the loop but hey it has a hard head and stubborn to do what I want it to do

Oh hang on... wait!!!! so what could be the problem coz what I understand is once the compiler gets to the if statement it just jump straight to the else part even if I entered the only last date and flight number it only reads when outside the loop, or shall I say I really dont know what could be the prob

One problem is that you are using the same variables to read from two different files.

for(int count = 0; count < Size && capeFile >> F.date; count++)
{
    //B1[count].setFlightDetail(F);
      capeFile >> F.FlightNum >> < all the rest here> ;
      cout << F.date << "   " << F.FlightNum << "\n";

    if(date == F.date && flight == F.FlightNum)
    {
	//Accept the passenger detail
    }
    else 
    {
	cout << "No match found" << endl;
    }
}
This article has been dead for over six months. Start a new discussion instead.