I dont mean to hijack the thread but maybe you could explain my problem as it seems to be on the same lines as jedi's. Basically i have written to a file and now im trying to read it back but when i do, i hit an infinite loop and the code breaks!!
I have put the IF statements before each print to try and solve the problem or atleast find where its breaking but to no luck!!
void inClientForm(customer* theRecord, int size)
int age, purchaseCount;
ifstream inClientFile( "clients.txt", ios::in );
cout << custname << endl;
inClientFile >> age;
cout << age << endl;
cout << gender << endl;
inClientFile >> purchaseCount;
cout << purchaseCount << endl;
inClientFile >> purchaseCost;
cout << purchaseCost << endl;
don't use eof to control the loop
>> I dont mean to hijack the thread but maybe you could explain my problem as it seems to be on the same lines as jedi's.
Please start a new thread and link to this one if you think it is related. Perhaps a moderator will separate your question for you. If so, this response can come along.
An infinite loop means an error has occurred while reading before the eof has been reached. Since your code does not check for read errors, it goes on forever attempting to read but failing because the stream is in a fail state. You need to check for successful read as 7stud pointed out. Simply using "while (inClientFile)" or "if (!inClientFile) then error out" should be good enough. You will still have to figure out why an error is occurring during the read.
My guess on why the error is occuring during the read is your mix of getline and operator>>. The getline function reads until it finds a newline character. The operator>> leaves any trailing whitespace in the input stream, including trailing newlines. At the end of your loop you read purchaseCost with operator>>. If there is a new line after the purchaseCost in the input file, then it will remain in the stream until the next time through the loop. The call to getline will then hit that newline and stop, leaving the custname empty and leaving the actual name in the input file stream. When you then attempt to read the age, it finds the name still there and fails because you cannot read a character into an int.
To solve that, add a inClientFile.ignore() after you read in purchaseCost (or after all calls to operator>> but not after getline). This will ignore trailing whitespace which will probably allow your getline to read in the next name properly.