Line 1: In C++, the proper object is an ifstream.
Line 2: Purpose of temp1, temp2, temp3, temp4 is unclear since the variable names are meaningless.
Line 10-13: These are out of place.
Line 11: No need for a line scanner. Just user the input operator (>>)
Line 16: n is undefined. Since you don't know how many records there will be, a
while
loop is more appropriate. This code is redundant since you're reading the file at line 20.
Line 20: Do not loop on !eof(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.
1 2 3
|
while (cin >> var)
{ // Good cin or istream operation
}
|
Line 22, 24, 26: The purpose of your if statements is not clear.
Line 24: C++ does not support implied left hand side in conditionals. You must fully specify the conditions.
Example:
if (ans == 'Y' || 'y')
evaluates as
if ((ans == 'Y') || ('y'))
('y')
always evaluates to 1 (
true
), therefore the if statement is always true.
Lines 22, 24, 26: You're using the assignment operator (=), not the comparison operator (==).
Line 30: Not clear which if this terminates.
Line 13,32-33: No need to explicitly dispose of objects unless they were created with new.