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;*/
}
}

Recommended Answers

All 6 Replies

lines 19-23 can be rewritten like below because it isn't necessary to test for eof directly. while( Infile >> N >> A >> G) delete line 63 if you recode as above

Other than the above, I don't know what kind of help you want.

I have done that but now nothing is really happening, everytime I try to compile it, it asks me to make a selection, I would like to post all my code but I have 2 cpp files and one .h file so I really do not think that would be wise other than that it is so damn irritating to read 20 pages of code

you can attach them to your post. Press the Go Advanced button then the Manage Attachments button that appears towards the bottom of the page.

Thanx Dragon.......

I have attached them

Two problems

1) don't use getche() -- instead use cin.get()

2) after receiving input for integers or a single character you are leaving the '\n' <Enter> in the keyboard buffer. That has to be flushed out because it causes great problems. The easiest way to do it is cin.ignore() But a more accurate method is described by Narue in her post at the top of this c++ board. Add that code in appropriate places and you problem will be solved.

3) You also need to change the loop in the Display() function like you did in the Edit() function.

commented: Im sure Reputations dont excite you anymore, ur used to them +1

Have you ever read something and after that feel like you dont know what you thought you knew or wander if there is anything u know about the subject at hand, thats what alwayz happen when I read Naure's Posts but I will look at it, many thanx dude

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.