Thread: getline function does not seem to activate

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Question getline function does not seem to activate

    As a complete beginner, I have just started working through "Jumping into C++". I am at the section on appending strings. The tutorial mentions the getline function but I can not seem to get it to activate. There is no mention of any other inclusions.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string user_first_name;
        string user_surname;
        string title;
        string address;
    
        double x = 0;
        double y = 0;
        double z = 0;
    
        cout << "Before we start, why not tell me your  first name : ";
        cin >> user_first_name;
        cout << "Ok, for the sake of formalities, what is your surname? : ";
        cin >> user_surname;
        cout << "And is that Mr, Mrs or Ms? : ";
        cin >> title;
        string user_full_name = title + " " + user_first_name + " " + user_surname;
        cout << "And what is your street address? : ";
        getline ( cin, address );
        cout << "Hi " << user_first_name << " nice to meet you.\n";
    I note that the getline function color remains black while other functions are green. I presume this means that Codeblocks has not associated it with any of the listed header files. Has the tutorial omitted this detail?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    No, you're fine. getline is defined in <string>.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    The compiler does not report any errors when I build however when run, the program skips from the results of line 24 to line 26. No user input for line 25 occurs.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    That is because the newline is left in the input buffer after your previous cin >> title;

    One solution is to get rid of that newline with cin.ignore. Another option is to read line by line with getline, then parse the string, e.g., using a stringstream.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You need to use ignore() when you switch from cin >> to getline, since they have different ideas as to what is a delimiter.

    Eg.
    Code:
    #include<iostream>
    #include<string>
    #include<limits>
    using namespace std;
    int main()
    {
      int a;
      string b;
      cin >> a;
      cin.ignore(numeric_limits<std::streamsize>::max(),'\n');
      getline(cin,b);
      cout << "a=" << a << endl;
      cout << "b=" << b << endl;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to activate Control scroll bars ?
    By Frandy in forum Windows Programming
    Replies: 1
    Last Post: 10-21-2004, 05:21 AM
  2. How to activate hor. & vert. scrollbars ?
    By Frandy in forum Windows Programming
    Replies: 4
    Last Post: 10-15-2004, 06:39 AM
  3. de/activate NIC by system interface/API calls?
    By joft in forum Windows Programming
    Replies: 1
    Last Post: 05-12-2004, 11:47 AM
  4. Really activate window on Win98
    By vn1 in forum Windows Programming
    Replies: 2
    Last Post: 02-04-2003, 02:42 AM
  5. how to activate printer
    By YALINI in forum C Programming
    Replies: 2
    Last Post: 01-31-2002, 02:25 PM