This is my code:

void removeAppointments()
  {
  vector <string> newfile;
  string line;
  int i;
  int n;
  ifstream Appointments;
  ofstream newAppointments;

  Appointments.open(DATAFILE);
  newAppointments.open(newDATAFILE);

  if (Appointments.is_open() && newAppointments.is_open())
    {
    // Read the file until it's finished, displaying lines as we go.
    while (!Appointments.eof())
      {
      getline(Appointments, line, '\n'); // getline reads a line at a time
      newfile.push_back(line);
      }

          while (n != i && n <= newfile.size())
            {
            n = 0;
            newAppointments << newfile[n] << '\n';
            n++;
            }
       
            
    }
  else   // Some kind of error opening the file
    {
    cout << "ERROR: Could not open the datafile(s)" << endl;
    exit(1);
    }
  
  newAppointments.close();
  Appointments.close();
  remove(DATAFILE);
  rename(newDATAFILE, DATAFILE);
 }

int main(int argc, char *argv[])
    {
    //Appointment appointment;
    int i = atoi(argv[1]) - 1;
    removeAppointments();
    }

However when I use the program, it simply deletes the entire datafile.

This is my code:

void removeAppointments()
  {
  vector <string> newfile;
  string line;
  int i;
  int n;
  ifstream Appointments;
  ofstream newAppointments;

  Appointments.open(DATAFILE);
  newAppointments.open(newDATAFILE);

  if (Appointments.is_open() && newAppointments.is_open())
    {
    // Read the file until it's finished, displaying lines as we go.
    while (!Appointments.eof())
      {
      getline(Appointments, line, '\n'); // getline reads a line at a time
      newfile.push_back(line);
      }

          while (n != i && n <= newfile.size())
            {
            n = 0;
            newAppointments << newfile[n] << '\n';
            n++;
            }
       
            
    }
  else   // Some kind of error opening the file
    {
    cout << "ERROR: Could not open the datafile(s)" << endl;
    exit(1);
    }
  
  newAppointments.close();
  Appointments.close();
  remove(DATAFILE);
  rename(newDATAFILE, DATAFILE);
 }

int main(int argc, char *argv[])
    {
    //Appointment appointment;
    int i = atoi(argv[1]) - 1;
    removeAppointments();
    }

However when I use the program, it simply deletes the entire datafile.

I assume you mean it doesn't delete the original file, rather the new file you produce is 0 bytes? The two obvious problems are your "i" and "n" variables. The first problem: You must pass "i" to your function removeAppointments. Right now you declare "i" inside main, so it is only visible to main. Therefore your function declaration should be void removeAppointments(int i).

The other problem is that you are setting n = 0 inside your while loop. How will "n" ever be as large as "i" if you set it to 0 at the beginning of each iteration? Move n = 0 to before the while loop starts.

On a side note: if (Appointments.is_open() && newAppointments.is_open()) with all of your code inside is fine, but it's a little strange for simple programs. Most programmers put those right after the open function. i.e.
fin.open(file);
if (!fin.is_open()) exit(1);
And then you can just simply write all your code after that and not worry about it.

Thanks I have done what you have told me so far.. However when I use the program and i remove the last line of the file, everything works perfectly. But when I, say, remove the second line of the file, all the lines after that second line also get removed...

I can kind of see why that might be myself.. due to the line

while (n != i && n <= newfile.size())

But I'm not sure how else to go about it.

Yeah if you break out of the loop once the "i" line is encountered, then obviously you won't write the rest of the file. You only want to skip one iteration of the loop. Also, you should have the condition "n < newfile.size()" and NOT "n <= newfile.size()". Why? Remember vectors and arrays start at 0, not 1. So if if you're allowing n to be equal to size(), well... what happens if for example the size() is equal to 1? In your vector, only newfile[0] would contain an element, but you'd be accessing newfile[1] which is out of range!

To skip only 1 line in a file, you would put:

while (n < newfile.size()){
  if (i == n) {n++; continue;}  //increments n and goes to beginning of loop
  newAppointments << newfile[n] << '\n';  
  n++;                     
}

Wow so the 'continue' allows the n to skip? Thanks for the tremendous help from both of you!

Hahahaahha you were too good I thought you had to be two different people :D

This question has already been answered. Start a new discussion instead.