Thread: getline

  1. #1
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179

    getline

    I'm working on an assignment and I'm trying to have the user enter a name, like this:
    Code:
        cout << "please enter this student's full name:\n>";
        cin.getline(search.name,strlength);
    This seems ok to me, but I get this error:
    lab4.cpp:185: invalid conversion from `int (*)(char*)' to `int'
    lab4.cpp:185: initializing argument 2 of `std::basic_istream<_CharT,
    _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, int) [with
    _CharT = char, _Traits = std::char_traits<char>]'
    185 is the getline line, and strlength is a global const int. I'm using gcc on a redhat linux 9, does anybody know what might be causing this?
    Last edited by ichijoji; 02-24-2004 at 02:26 PM.
    Illusion and reality become impartiality and confidence.

  2. #2
    Registered User trainee's Avatar
    Join Date
    Jan 2004
    Posts
    32
    Make the name member a std::string and use
    Code:
    std::cin >> search.name;
    This is why it's always best to use the standard, it makes a lot of tasks easier.

    trainee
    Tux says hello.

  3. #3
    Registered User Paz_Rax's Avatar
    Join Date
    Jan 2004
    Posts
    16
    Isn't the syntax for cin.getline
    cin.getline( *buffer_name, int_length, "charTOstopAT") as in:
    Code:
    cin.getline( *buffer, 256 , '\n' )
    ?
    "Life, it's all in how you script it."

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You should probably show more code, like the declaration of search.name and strlength, as well as what headers you are including. Also, I assume you are using g++ to compile.

    This code seems to work just fine:
    Code:
    #include <iostream>
    using namespace std;
    
    const int strlength = 80;
    
    int main()
    {
        char name[strlength];
        cout << "please enter this student's full name:\n>";
        cin.getline(name,strlength);
    }
    Incidentally, the standard string version of that code is this:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string name;
        std::cout << "please enter this student's full name:\n>";
        std::getline(std::cin, name);
    }

  5. #5
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    Also, I assume you are using g++ to compile.
    Nope! haha I'm an idiot, g++ compiles it just fine.
    Illusion and reality become impartiality and confidence.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 11:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM