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");
}