how to implement the changePatient() function

I have to create a program that enable insert of patient record and edit patient record when prompt. The two function suppose to read data from and write data to data file (eg: patient.txt)
When the connection to data file is open, user will able to edit the data inside the text file and save it again so program will be able to display updated data. Now the problem is I don't know how to implement the changePatient() function with the data file. Below is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void change_patient()            //change the name, appointment benefit, or drug benefit of a patient
           {
               ofstream changePatient;
               
               do{
                    
                    changePatient.open("Patient.txt", ios::app);
                    if (! changePatient)
                    {
                          cout<<" Error! The file you request does not exist.";
                          }
                    else{
                    cout<<"Select the patient you wish to edit.";
                    }
                    if(changePatient.close())
                    {
                    cout<<"Save record?";             //Save Record
                    cin>>choice;
                    }
                    if((choice == 'Y') || (choice == 'y'))
                    {
                    changePatient<<patientID<<"\t"<<patientName<<"\t"<<a_benefit<<"\t"<<d_benefit<<endl;
                    cout<<"Your record has been saved successfully!"<<endl;
          
                    }
                    else if((choice == 'N') || (choice == 'n'))
                                {
                                  cout<<"Do you want to add another patient?: "<<endl;
                                  cin>>choice;              //Enter another patient record
                                  }
						}while ((choice == 'Y')||(choice == 'y'));          //while user input 'Y' or 'y'
               }


I not sure how to allow user to edit the text file by giving some prompt operation. Can anyone sort me some idea?
The object that you use to both read from and write to a file is an "fstream": http://www.cplusplus.com/reference/iostream/fstream/

Do you know how to use a class yet? This would make it a whole lot easier, but if not then we can still work with you.

Line 8 should be changed to check the 'fail' bit of the stream object, or test to see if the file is open. http://www.cplusplus.com/reference/iostream/ofstream/ofstream/

Topic archived. No new replies allowed.