Thread: Reading in vectors

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    81

    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?

    Code:
    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+ " " +
    							Access[i].middleIni;
    	
    
    	Add(Access, Process, i);
    
    } // End FullName function

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    vector

    why not just write..?

    Code:
    string s;
    cin.getline(s,50);
    access.insert(x, s);
    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..

    Code:
    int *ptr;
    cout << "Insert array size: ";
    cin >> size;
    ptr = new int[size];
    telling u this because with vectors i don't believe u can write
    acces[5] = "bob";
    though i am not positive as i just used vectors for first time yesterday.. good luck

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    vector <string> names;
    //names has a default capacity of unknown quantity and size 0.

    string name;
    cout << "enter your first and last names" << endl;
    getline(cin, name);
    //getline is overloaded for use with STL strings. The syntax is
    //slightly different than for Cstyle strings.

    names.push_back(name);
    //push_back() a common mechanism to add to the vector, though
    //there are others

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    yep many others...

    perhaps this may be of help..
    Code:
    ---------------------
    iterator 
    insert(iterator position, const T& x);
    
    Inserts x before position. The return value points to the inserted x.
    ------------------
    void 
    insert(iterator position, size_type n, const  T& x);
    
    Inserts n copies of x before position.
    -----------------------------
    void 
    insert(iterator position, InputIterator first, 
    InputIterator last);
    
    Inserts copies of the elements in the range [first, last] before position.
    --------------------------
    void 
    push_back(const T& x);
    
    Inserts a copy of x to the end of self.
    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...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Resetting a ifstream object after reading the whole file
    By Zeeshan in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2008, 08:03 AM
  3. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  4. help with reading from stream
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 10:36 PM
  5. Fun with reading hex
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2006, 06:41 PM