The code below suppose to ask a user the person's name and compare that name with the names in the file and if found it must display the name's details such as Age and gender and ask the user if s/he wants to edit the person's details if the user says yes then the program must accept the name, age and gender from the user and save or write to the Temp file at the end it has to destroy the Temp file and keep everything else to the info files but it just jump and close the file and attempt to remove and rename it without searching from the existing file which is "Info.dat" any help will be appreciated
void Edit()
{
ifstream inFile;
ofstream TempFile;
string searchName;
string N, NewName;
int A, NewAge;
char G, NewGender;
char Answer;
inFile.open("Info.dat");
if(inFile)
{
TempFile.open("Temp.dat");
cout << "Enter the person name Please: ";
cin >> searchName;
inFile >> N;
while(inFile)
{
inFile >> A;
inFile >> G;
if(searchName.compare(N) == 0)
{
cout << "Name: " << N << endl;
cout << "Would you like to edit (y/n)? ";
cin >> Answer;
if((Answer == 'Y') || (Answer == 'y'))
{
cout << "PLease enter the new name: ";
cin >> NewName;
}
else
NewName = N;
cout << "Age: " << A << endl;
cout << "Would you like to edit (y/n)? ";
cin >> Answer;
if((Answer == 'Y') || (Answer == 'y'))
{
cout << "PLease enter the new age: ";
cin >> NewAge;
}
else
NewAge = A;
cout << "Gender: " << G << endl;
cout << "Would you like to edit (y/n)? ";
cin >> Answer;
if((Answer == 'Y') || (Answer == 'y'))
{
cout << "PLease enter the new gender: m-Male, f-Female: ";
cin >> NewGender;
}
else
NewGender = G;
{
TempFile << N << endl;
TempFile << A << endl;
TempFile << G << endl;
}
inFile >> N;
}
inFile.close();
TempFile.close();
int OK = remove("Info.dat");
if(OK == 0)
{
cout << "Info.dat successfully deleted" << endl;
int OK2 = rename("Temp.dat", "Info.dat");
if(OK2 == 0)
cout << "Successfully rename Temp.dat as Info.dat" << endl;
else
cout << "could not rename Temp.dat as Info.dat" << endl;
}
else
cout << "Info.dat could not be deleted" << endl;
}
/*else
cout << "Error in oppening the file" << endl;*/
}
}