When I want to access a part of the vector (appointment) it won't allow it, for example simply adding:

cout << appointment[1];

will not work.

I also want this vector to be saved to my class (Appointment), so I can later remove elements of the vector. Would I be able to do this simply by declaring the vector in the class? i.e.

class Appointment
         {
         private:
         vector <string> appointment;
         }

This is my whole code:

void Appointment::loadAppointments()
  {
  ifstream Appointments;
  string line;
  vector <string> appointment;
  int i = 1;

  cout << "You have the following appointments:" << endl;

  Appointments.open(DATAFILE);
  if (Appointments.is_open())
    {
    // Read the file until it's finished, displaying lines as we go.
    while (!Appointments.eof())
      {
      getline(Appointments, line, '\n');
      appointment.push_back(line); 
      }
    Appointments.close();
    }
    cout << "this is a test" << appointment[1] << endl;//This part will not function
  }

Edited 5 Years Ago by ksm092: n/a

The code looks okay except that you have a local variable shadowing a member variable. I imagine you want to delete line 5. Sounds like you're planning on deleting that line anyway. Seems like a good idea.

Regardless, that should not cause the problem you're getting. Line 21 should still print assuming the file is good, I would add a debugging line after line 16 that displays the "line" variable. If that works twice and prints what it should, I think line 21 should print right. If it doesn't, line 21 will fail. You'll want to do some testing to make sure that element exists before you try to access it. If you're NOT sure it exists, it might be safer to use the "at" function instead of [].

The difference between this member function and member operator function operator[] is that vector::at signals if the requested position is out of range by throwing an out_of_range exception.

http://www.cplusplus.com/reference/stl/vector/at/

This was sort of a test code so I didn't add the original cout, reading the datafile line by line and so I know that there are at least 5 line variables. However I don't understand why it still isn't working..

For my previous question about being able to edit the same vector in other programs using a class, would that simple declaration be suffice? or is there some other method or more things I need to do?

>> This was sort of a test code so I didn't add the original cout, reading the datafile line by line and so I know that there are at least 5 line variables. However I don't understand why it still isn't working..

I don't either. If you have "line" printing out correctly five times when you have it in there as a debugging statement, then it seems like line 21 should print the second line. What precisely happens when you add this line after line 16?

cout << line << endl;

and this line before line 21?

cout << appointment.size() << '\t' << appointment.capactity() << endl;

>> For my previous question about being able to edit the same vector in other programs using a class, would that simple declaration be suffice? or is there some other method or more things I need to do?

The devil is in the details, but it seems to me that you should probably be able to delete line 5 and replace it with what you have at the top and not do anything else. You might want to replace line 5 with "appointment.clear()" to make sure it's empty every time you read into the file. If that is in fact what you want.

But keep it the way it is now till it works. Seems like it should. If your file is this:

Sam
Bob
Fred
Dave
Tim

it seems like line 21 should display "Bob".

Edited 5 Years Ago by VernonDozier: n/a

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