curiosity about cin.get() and cin.getline()

This is a discussion on curiosity about cin.get() and cin.getline() within the C++ Programming forums, part of the General Programming Boards category; when i started to pick up studying again i had almost forgot in code after you use cin.get() you have ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    curiosity about cin.get() and cin.getline()

    when i started to pick up studying again i had almost forgot in code after you use cin.get() you have to immediately follow it up with another to allow more input why is this im just curious why for 2 inputs you need 3 cin.get()? ok thanx i was just wondering bye
    hooch

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Not entirely sure what you mean.

    Are you referring to the user pressing enter, or to '\0' ?

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    good example would be this problem ive run into:

    char dummy[30];
    char dummy2[30];
    cout << "ENTER whatever you want up to 30 cahracters, do it twice hit enter inbetween\n";
    cin.get(dummy,30);
    cin.get();
    cin.get(dummy2,30);


    that middle cin.get()

    y does that have to be there to get access to the dummy2 input cuz if i do this:

    cin.get(dummy,30);
    cin.get(dummy2,30);

    it skips over the dummy2 input part y is that?
    hooch

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Use a little punctuation PLEASE!!!!! It's impossible to read your posts.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    my second post seems pretty clear what dont you understand? basically all it saids is

    cin.get(var1,10);
    cin.get();
    cin.get(var2,10);

    why does the middle cin.get() have to be there in order to get access to the var2 input part?
    hooch

  6. #6
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    There's probably something left in the input stream. Your allow up to 30 chars in the input, but your buffer is only 30 chars long. The null-termination can't fit, and that's why you need to the cin.get().
    I may be wrong, are you using the whole 30 chars in your input? If you aren't it may be because of something else.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    no the whole 30 characters to be allowed is to test run it incase it gets messed up jus kinda a to avoid it crashing cuz of lack of letters allowed
    hooch

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >There's probably something left in the input stream.
    Yes, there is. cin.get() reads up to but not including a newline. The next call to cin.get() is not the same get function as before, this one reads a single character no matter what it is, eating the newline and discarding it. The next call to cin.get() does the same thing as the first call.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    can you detail that explanantion a little more that kinda confused me in all ways lol?
    hooch

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Which compiler are you using?

  11. #11
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    visual c++ 6.0
    hooch

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Originally posted by ssjnamek
    can you detail that explanantion a little more that kinda confused me in all ways lol?

    cin.get(char[], int) fills the character array, except for the newline that was generated when the user pressed 'enter'. It ignores this newline, and leaves it in the input stream. If you call cin.get(void) directly after that, it will grab that newline, causing the program to continue (and in your case, exit immediately).

    The easiest remedy is to use cin.getline(char[], int). That function also fills the character array excluding the newline, but instead of leaving the newline in the input stream, it discards it.

    Problem solved.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    maybe I'm reading Sebastini's reply wrong, but getline() seems a little more complicated than that. As I understand getline() t will read in a certain number of char (designated by the second parameter) into a char array (designated by the first parameter) unless the third parameter, called the terminating char which is defaulted in the prototype to newline char, is found. if input into the char array is terminated because the terminal char is identified, then the terminating char is removed from the input buffer. Therefore, if a newline char remains in the input buffer after a call to get() that newline char is the first char in the buffer. When you call getline() using the default terminating char it will see the newline, terminate input into the current char array, remove the first newline from the input buffer, but put nothing else current buffer. getline() will not ignore the first newline char unless you use some char other than newline as the terminating char. A second call to get() may ignore the leading newline char, much like sequential calls to >> will, I'm not sure, but if you want to remove newline from the char buffer using getline() I'm almost positive it has to be the terminating char, and therefore any leading newline won't be ignored.

    When you want to ignore something left in the input buffer, I'd suggest using ignore(). The common place to use this is after calls to >> and before a subsequent call to getline() when you want to use a whitespace char to terminate getline() and the whitespace char was left in the input buffer by the call to >> (or get()??).
    Last edited by elad; 11-29-2003 at 12:25 PM.

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Try this and see for yourself:

    Code:
    int main()
    {
     char buffer[100] = {0};
     
     cout << "Just press enter." << endl;
     
     cin.getline(buffer, 100);
     
     cout << "->" << buffer << "<-" << endl;
     
     cin.get();
    }
    Had the newline been stored in the buffer, the output would've been:

    ->
    <-
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #15
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    whats the = (0) about?
    hooch

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

Similar Threads

  1. cin.getline help
    By Cool Dude 2k in forum C Programming
    Replies: 2
    Last Post: 07-27-2005, 07:55 PM
  2. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 12:01 PM
  3. cin.getline()
    By Ifurita. in forum C++ Programming
    Replies: 4
    Last Post: 05-29-2003, 02:14 PM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 06:53 PM

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