Thursday, July 19, 2018

C++ while loop to read from input file



I wrote a function that reads in transactions from an input file using a while loop. I can't for the life of me figure out why it is reading the last 2 lines twice though. When using



 while(InFile){code}


from my understanding it will continue looping until the file as reached the EOF marker. I can't figure out where I am going wrong here.



void ProcessTransactions(Bank &acctList, string fileName)

{

Date transDate;
ifstream InFile;
InFile.open(fileName.c_str());
int month;
int day;
int year;
int acctNum;
int transAcctNum;

float amount;
string transType;

while(InFile)
{
InFile >> month >> day >> year;
transDate.SetDate(month, day, year);

InFile >> acctNum;
InFile >> amount;

InFile >> transType;
if(transType == "Transfer")
InFile >> transAcctNum;

cout << amount << endl;
}
}


Input File




5 1 2012    1212    100.00  Deposit
5 1 2012 2323 100.00 Deposit
5 1 2012 3434 100.00 Deposit
6 1 2012 1212 200.00 Withdrawal
6 1 2012 2323 200.00 Withdrawal
6 1 2012 3434 50.00 Withdrawal
7 1 2012 1212 50.00 Transfer
2323
7 1 2012 2323 80.00 Transfer

3434
7 1 2012 3434 300.00 Transfer
1212
9 1 2012 1212 100.00 Deposit
9 1 2012 2323 100.00 Deposit
9 1 2012 3434 100.00 Deposit
10 1 2012 1212 300.00 Transfer
1212



outputs



100
100
100
200
200
50
50
80

300
100
100
100
300
300 //** Why is this output twice ?


After it extracts the last bit of data, the file marker should have reached EOF, thus terminating the loop.




Any help would be much appreciated!



=========================================================================
Additional EXPLANATION / Solution :
from:
Why is iostream::eof inside a loop condition considered wrong?



Because iostream::eof will only return true after reading the end of the stream. It does not indicate, that the next read will be the end of the stream.



Consider this (and assume then next read will be at the end of the stream)




while(!inStream.eof()){
int data;
// yay, not end of stream yet, now read ...
inStream >> data;
// oh crap, now we read the end and *only* now the eof bit will be
set (as well as the fail bit)
// do stuff with (now uninitialized) data
}



Against this:



int data;
while(inStream >> data){
// when we land here, we can be sure that the read was successful.
// if it wasn't, the returned stream from operator>> would be
// converted to false
// and the loop wouldn't even be entered
// do stuff with correctly initialized data (hopefully)

}

Answer




After it extracts the last bit of data, it the file marker should have reached EOF, terminating the loop.




No.



EOF is set when you attempt to read past the end of the file. Here you do not check that your extraction succeeded, only that the stream is okay before you attempt extraction. Hence you'll get an extra iteration at the end.




You should loop like this (and there are plenty of examples of it on , since we are constantly telling people how to do it):



while (InFile >> month >> day >> year)

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...