Reading in vectors
Can anyone tell me how to read in a string into a vector? I know that if I use cin (as I did below) it will not read whitespaces. But if I use cin.getline, cin.get or gets it gives me an error because of the subscript (which I need) and if I take it out, it tells me that my members are not part of my vector!
I've had this problem before but for some reason I always draw a blank when I encounter it. Can anybody point me in the right direction?
void FullName(vector<Names>& Access, vector<Entry>& Process, int& i)
// Pre condition: User has entered A in the menu
// Post condition: will return the full name to the Add function
cout << "\nYou have chosen to add an entry.\n\n";
cout << "Please enter the last name: ";
cin >> Access[i].lastName;
cout << "\n\nPlease enter the first name: ";
cin >> Access[i].firstName;
cout << "\n\nPlease enter the middle initial: ";
cin >> Access[i].middleIni;
Process[i].fullName= Access[i].lastName+ " " +
Access[i].firstName+ " " +
Add(Access, Process, i);
} // End FullName function
why not just write..?
if u want to do it the way u r trying to.. why not just use an array?
if u want an array with undefined length create it dynamically and reallocate memory.. or..
telling u this because with vectors i don't believe u can write
cout << "Insert array size: ";
cin >> size;
ptr = new int[size];
acces = "bob";
though i am not positive as i just used vectors for first time yesterday.. good luck
vector <string> names;
//names has a default capacity of unknown quantity and size 0.
cout << "enter your first and last names" << endl;
//getline is overloaded for use with STL strings. The syntax is
//slightly different than for Cstyle strings.
//push_back() a common mechanism to add to the vector, though
//there are others
yep many others...
perhaps this may be of help..
just some vector functions for inserting stuff.. there are more but i don't want to make this thread too huge.. just look in .h file for the rest mate...
insert(iterator position, const T& x);
Inserts x before position. The return value points to the inserted x.
insert(iterator position, size_type n, const T& x);
Inserts n copies of x before position.
insert(iterator position, InputIterator first,
Inserts copies of the elements in the range [first, last] before position.
push_back(const T& x);
Inserts a copy of x to the end of self.