Like Tree1Likes
  • 1 Post By Salem

getline function does not seem to activate

This is a discussion on getline function does not seem to activate within the C++ Programming forums, part of the General Programming Boards category; As a complete beginner, I have just started working through "Jumping into C++". I am at the section on appending ...

  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,032
    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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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;
    }
    King Mir likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 01:42 AM
  5. how to activate printer
    By YALINI in forum C Programming
    Replies: 2
    Last Post: 01-31-2002, 01:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21