-
Flush problem
When i load data into an array, I want it to find errors from the input (invalid character for quantity and price). I know a flush should be used to "flush" the rest of the line and an attempt to continue to the next line.
The code i wrote is:
Code:
...
fsinventoryFile>> invArray[index].itemCode;.
...
...
while(!fsinventoryFile.eof() )
{ //begin read from file
fsinventoryFile >> invArray[index].quantity
>> invArray[index].price;
cout<<"Record " <<index << " item code is:\t " <<invArray[index].itemCode <<endl;
cout<<"Record " <<index << " quantity is:\t " <<invArray[index].quantity<<endl;
cout<<"Record " <<index << " price is:\t " <<invArray[index].price<<endl;
while(!(invArray[index].price ) || (fsinventoryFile >> invArray[index].quantity))
{
cin.clear();
while(cin.get(aCh) && aCh !='\n'); //flush
cout <<"Invalid input on: " <<invArray[index].itemCode
<< flush<<endl;
}
index++;
fsinventoryFile>> invArray[index].itemCode;
}//end-while
Any help of this would be apprecited.....thanks!
-
So what exactly is your question? :confused:
-
My questions is that when i run it, the flush doesn't work. It does not find the invalid characters for the quantity or price and does not move on to the next line.
-
Your problem (well, one of them) is more than likely on this line:
Code:
while(!(invArray[index].price ) || (fsinventoryFile >> invArray[index].quantity))
Going back a few lines, this:
Code:
fsinventoryFile >> invArray[index].quantity >> invArray[index].price;
... will probably not touch the variables if the input data is bad. It will not set quantity to 0 upon reading a character.
-
I tried a different approach but it still is not working....for example if the quantity in the file is "10" and someone wrote "1F" then it should show an error msg and flush out that line...so far no luck..
A book im using has an example like this...but still not a big help
Code:
while(!(cin >>invArray[index].price))
{ cin.clear();
while(cin.get(aCh) && aCh !='\n'); //looks for error
cout <<"Invalid input on: " << invArray[index].itemCode;
}
while(!(cin >> invArray[index].quantity))
{
cin.clear();
while(cin.get(aCh) && aCh !='\n');
cout <<"Invalid input on: " << invArray[index].itemCode;
}
Any other advice out there or changes i should make to this code??
-
Here is the file with the input just in case you need more information and/or interested in helping an unexperienced c++ programer....it would help me huge.
-
Here is the input file....