0

Before adding Ken's points Display After adding Ken's points

As you can see after saving ken's points to the text file, it also created a copy of the last line. Not sure what happen here but here's my codes for this display.

If I repeatedly use this again and again it will create many of the last lines at the end of the text file. Any solutions how to stop this from happening?

{
ifstream myFile;
myFile.open("StaffList.txt");
ofstream temp;
temp.open("temp.txt");
int i=1, code, points, InputPoints;
fstream inFile;
string ID, NRIC, InputID;
double price, countPrice, total=0.0, after_discount;
bool found=false;

cout << endl;
cout << "\t\t\t      ******************* " << endl;
cout << "\t\t\t      **  Staff Order  ** " << endl;
cout << "\t\t\t      ******************* " << endl << endl;
cout << "Enter number of item(s): ";
cin >> countPrice;

for(int i=0; i< countPrice; i++)
    {
        cout << "Enter Price" << i+1 << ": $";
        cin >> price;
        total = total + price;

    }
after_discount = 0.5*total;
cout << endl;
cout << "Total Price: $" << total << endl;
cout << "50% Discount! " << endl;
cout << "Total Payment after Discount: $" << after_discount << endl;

if (after_discount >= 1)
{
    InputPoints= after_discount;
    cout <<setprecision(0) <<fixed<< "Accumulated Points: " << InputPoints << endl << endl << endl;
}

cout<<"Name of Staff: ";
cin >> InputID;

while (!myFile.eof())
{
    myFile>>ID>>NRIC>>code>>points;
    if (ID == InputID)
    {
        points=InputPoints+points;
    }   
    temp<<ID<<" "<<NRIC<<"  "<<code<<"  "<<points<<"\n";
}

myFile.close();
temp.close();
remove("StaffList.txt");
rename("temp.txt", "StaffList.txt");

myFile.open("StaffList.txt");
while (!myFile.eof())
{
    myFile>>ID>>NRIC>>code>>points;
    if (ID == InputID)
    {
        cout<<"Data Updated!\n";
        cout<<ID<<" "<<NRIC<<"  "<<code<<"  "<<points<<"\n";
    }
}
myFile.close();
system("pause");
system("cls");

}

KYLE LEE
  • 31
  • 1
  • [`while (!myFile.eof())`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user657267 Feb 03 '15 at 03:28
  • You should check if the line is empty in your while loop I think. –  Feb 03 '15 at 03:33
  • Sorry, I'm very new to c++, started like a month ago. What do you mean by "check if the line is empty"? – KYLE LEE Feb 03 '15 at 03:45

0 Answers0