Thread: vectors and cin, getline?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    83

    vectors and cin, getline?

    hi,

    for some reason the line "cout << "Enter your name : "; getline(cin, name);" is acting up. can someone please check it out for me? i'm using it with vectors and a class.

    file.cpp
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include "file.h"
    using namespace std;
    
    int my_class::askUser()
    {
    	cout << "Enter your name : ";	getline(cin, name);
    	cout << "building number : ";	cin >> build_num;
    	cout << "apartment number: ";	cin >> apt_num;
    //	cout << "Enter your name : ";	getline(cin, name);     //placing it here gives me the same problem
    return 0;
    }
    
    void my_class::dispInfo() const
    {
    	cout << "customer name is "		<< name			<< endl;
    	cout << "building number is "	<< build_num	<< endl;
    	cout << "apartment number is "	<< apt_num		<< endl;
    }
    
    int main()
    {
    vector<my_class> one(6); int i;
    
    		for(i=0; i<one.size(); i++)
    		{
    		one[i].askUser();
    		one[i].dispInfo();
    		}
    
    return 0;
    }
    file.h
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class my_class
    {
    public:
    	int askUser();
    	void dispInfo() const;
    
    private:
    	int build_num;
    	int apt_num;
    	string name;
    };
    on run, after the first execution of the loop, it should ask for the the string input again but it skips it. i have no idea how to correct it. the logic is perfect i think.

    thanks,
    barneygumble742

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    >> doesn't consume the newline from the input. getline snaps it up and immediately returns. I suppose that's your problem.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    so what can i use besides getline that will work? i would like to try getc but isn't that only for characters?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Continue to use getline, but after you call cin >> you must ignore the extra newline. The simple way to do that is to add cin.ignore(); between a call to cin>> and a call to getline.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    could you explain where to put the cin.ignore(); ?

    i put it at the very top and after the getline and both ways it acts very weird.

  6. #6
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    cout << endl;
    cin >> myvariable;
    cin.ignore();
    cout << endl;
    getline(cin, myvariable2);

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    read this for more detail.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    hi...this is what i did and it works fine and i'm able to put it in the member function regardless the location. i don't know why i didn't think of this to begin with.

    Code:
    int my_class::askUser()
    {
    string first, last;
    	cout << "Enter your name : ";	cin >> first >> last;
    		name = first + " " + last;
    	cout << "building number : ";	cin >> build_num;
    	cout << "apartment number: ";	cin >> apt_num;
    return 0;
    }
    thanks again everyone. i'm keeping all your recommendations.

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    int my_class::askUser()
    {
    string first, last;
    	cout << "Enter your name : ";	cin >> first >> last;
    		name = first + " " + last;
            cin.ignore();   //catch the return key so you dont leave it in the stream
    	cout << "building number : ";	cin >> build_num;
    	cout << "apartment number: ";	cin >> apt_num;
    return 0;
    }
    OR...
    Code:
    int my_class::askUser()
    {
    string name;
    	cout << "Enter your name : ";	getline(cin, name);
    	cout << "building number : ";	cin >> build_num;
    	cout << "apartment number: ";	cin >> apt_num;
            cin.ignore();   //catch the return key so you dont leave it in the stream
    return 0;
    }
    Last edited by JoshR; 07-01-2005 at 11:33 PM.

  10. #10
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by barneygumble742
    hi...this is what i did and it works fine and i'm able to put it in the member function regardless the location. i don't know why i didn't think of this to begin with.

    Code:
    int my_class::askUser()
    {
    string first, last;
    	cout << "Enter your name : ";	cin >> first >> last;
    		name = first + " " + last;
    	cout << "building number : ";	cin >> build_num;
    	cout << "apartment number: ";	cin >> apt_num;
    return 0;
    }
    thanks again everyone. i'm keeping all your recommendations.
    You will still have the same problem as before, you are leaving the return character in the stream. But now since all your variables are taken care of, it doesnt seem appearant to you. Just read everyones post, I think cin.ignore(); should be stuck in your brain by now.
    Last edited by JoshR; 07-02-2005 at 01:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Vectors (cont) - Sort Problem
    By clegs in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2007, 06:31 AM
  2. extreme frustration with vectors
    By n3v in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2006, 09:39 PM
  3. vectors and user input
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2005, 10:23 AM
  4. Real quick question on vectors, if I may
    By hpy_gilmore8 in forum C++ Programming
    Replies: 7
    Last Post: 05-13-2003, 09:01 AM
  5. Reading in vectors
    By stimpyzu in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2002, 03:28 PM