get and getline

This is a discussion on get and getline within the C++ Programming forums, part of the General Programming Boards category; I am confused. When I use getline() everything works fine. But when I change it to get(), I get an ...

  1. #1
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61

    get and getline

    I am confused. When I use getline() everything works fine. But when I change it to get(), I get an error message stating it was not declared in this scope:


    getline(input, nonterm[index2][++index], TERMINATE ); //works fine.

    get(input, nonterm[index2][++index], TERMINATE ); //error get not declared in this scope.


    The reason I am changing getline() to get() is because I don't want the delimiter discarded. Any help would be great. Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I was not aware there was such a function as get(). Where did this function come from, and did you include the header for it?

  3. #3
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    I read it here:

    http://www.cplusplus.com/reference/i...tream/get.html

    I have these header files:
    #include<iostream>
    #include<fstream>
    #include<string>

    getline works perfect except for the fact that I need to keep the delimiter from being discarded.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And did you look at the example on that page? Did you notice how get was called?

  5. #5
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    When I try to use like this:

    input.get( nonterm[index2][++index], TERMINATE )

    I get tons of errors. And getline didn't work that way either. It worked as:

    getline(input, nonterm[index2][++index], TERMINATE );

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm guessing, just by names, that nonterm[index2][++index] is the character you want to read into? If you want to use .get on a character, then you cannot use other parameters (it will only read one character, so delimiter doesn't make any sense). If nonterm[index2][++index] is a c-string/char array, then you also need to pass the size.

    (The meanings of the parameters are given at the top of the page -- the way you call it has to match one of those five listed.)

  7. #7
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    Thanks for helping me. nonterm[index2][++index] is a string-array.

    I believe I am calling it as: istream& get ( streambuf& sb, char delim );
    when I call it as input.get( nonterm[index2][++index], TERMINATE )

    even though that was not how getline was called when it worked. Here are my errors. I am not sure what they mean:


    prog1.cpp:89: error: no matching function for call to ‘std::basic_istream<char, std::char_traits<char> >::get(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const char&)’

    /usr/include/c++/4.1.3/istream:272: note: candidates are: typename std::basic_istream<_CharT, _Traits>::int_type std::basic_istream<_CharT, _Traits>::get() [with _CharT = char, _Traits = std::char_traits<char>]

    /usr/include/c++/4.1.3/istream:286: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT&) [with _CharT = char, _Traits = std::char_traits<char>]

    and more.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    By string-array, do you mean an array of std::string? You cannot use get to read in a std::string. Even if you mean an array of chars, you cannot pretend an array of chars is a streambuf& -- only stringbuf and stringstream would work there.

  9. #9
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    nonterm was declared as a double pointer: string** nonterm;

  10. #10
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    But how could getline work and get not work?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, look at getline:
    Code:
    istream& getline ( istream& is, string& str, char delim );
    getline is designed to read into a std::string. get is designed to read into a C-style string (array of char) or into a stringstream/stringbuf object.

  12. #12
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    What about using getline() and then appending the terminating character to the string?
    or
    use
    Code:
    istream& get ( char& c ); // or
    int get();
    and write your own function to read in a line which will include the delimiter.

    example:
    Code:
    istream& get(string& fill, char term){
       char c;
       do {
          get(c);
          if(cin.fail()) break; // make sure to properly check for failure
          fill.append(1, c);
       }while(c != term);
       return cin; // I'm not fully sure if this line will work
    }

  13. #13
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    If I went the stringstream/stringbuf route would I just declare my string** nonterm as a stringstream** nonterm?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by lord View Post
    If I went the stringstream/stringbuf route would I just declare my string** nonterm as a stringstream** nonterm?
    You could. What I would probably do is something like this:
    Code:
    std::stringstream temp;
    input.get(temp, TERMINATE);
    temp >> nonterm[whatever_it_was][index++];
    so that you still have strings elsewhere in your code, where you probably want them.

    (Edit: This assumes rather that there aren't spaces in your input, as that would wreak havoc, but if that's a problem you could probably do a getline from temp or something similar.)

  15. #15
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    There are spaces so I use getline once I get this working. The problem now is I am getting an error stating:

    prog1.cpp:95: error: invalid conversion from ‘void*’ to ‘char*’
    prog1.cpp:95: error: initializing argument 1 of ‘std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]’

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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