Getline not wanting to work, for me

This is a discussion on Getline not wanting to work, for me within the C++ Programming forums, part of the General Programming Boards category; Hi, would need help with another little problem: getline. I wrote a small function to make it write to hero_name ...

  1. #1
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76

    Question Getline not wanting to work, for me

    Hi, would need help with another little problem: getline.
    I wrote a small function to make it write to hero_name but it gives me errors.

    Code:
    void character::namehero(){cin.getline(hero_name,15,'\n');}
    I:\C++\[OOP]T-RPG\player.cpp||In member function 'void character::namehero()':|
    I:\C++\[OOP]T-RPG\player.cpp|6|error: no matching function for call to 'std::basic_istream<char, std::char_traits<char> >::getline(std::string&, int, char)'|
    d:\james\programming\codeblocks\mingw\bin\..\lib\g cc\mingw32\4.4.1\include\c++\istream|593|note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]|
    d:\james\programming\codeblocks\mingw\bin\..\lib\g cc\mingw32\4.4.1\include\c++\istream|405|note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]|
    ||=== Build finished: 1 errors, 0 warnings ===|

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    cin.getline is not for strings, it is for character arrays. Call std::getline (from <string>) instead.

  3. #3
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    OH,ok!
    I'll try that and come back with the answer

    Edit: does it work in the same way or is the syntax diferent?
    Isn't it <cstring>, for C++?

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    #include <string> for C++ standard library strings. #include <cstring> for C-style character arrays (strcmp, strcpy, etc.)

  5. #5
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    Ok, now for the first question: "does it work in the same way or is the syntax diferent?"

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651

  7. #7
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    Thanks will try it out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  2. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 03:48 AM
  3. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 10:53 AM
  4. Help with getline() and file reading
    By evilkillerfiggi in forum C++ Programming
    Replies: 5
    Last Post: 10-15-2005, 03:49 PM
  5. Developers Wanted
    By Quasicom in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 08-24-2005, 12:46 AM

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