Thread: cin.get vs cin.getline

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463

    cin.get vs cin.getline

    Hi,
    Can you help me? I stumbled upon this rather rudimentary behavior yet unable to explain why it happens. I hope someone here can help me unravel this:

    Code:
    #include <iostream>
    #include <string>
    using namespace std; 
    
    int main()
    {
      string s1; 
      string s2; 
      int years;
      int months;
      char address[90];
      
      cin >> years;
      cin >> months; 
      cin.get(address,90);
      
      cout << "Enter string 1: ";
      cin >> s1; 
      cout << "Enter string 2: ";
      cin >> s2; 
      cout << s1 << endl;
      cout << s2 << endl; 
      return 0; 
    }
    For the code above, I use cin.get to obtain the value for 'address'; because of the newline character in queue, 'address' will get the newline character instead. Curiously, this will also skip over the acquisition of 's1' and 's1' as well. Moreover, if i replace: cin.get(address,90) with
    Code:
    cin.getline(address,90)
    The program will not skip over 's1' and 's'2'. Why is that?

    Sincerely yours,

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well let's look at cin::get(char *, streamsize) in the manual:

    istream& get (char* s, streamsize n );
    Extracts characters from the stream and stores them as a c-string into the array beginning at s. Characters are extracted until either (n - 1) characters have been extracted or the delimiting character '\n' is found. The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation.
    If the delimiting character is found, it is not extracted from the input sequence and remains as the next character to be extracted. Use getline if you want this character to be extracted (and discarded).
    The ending null character that signals the end of a c-string is automatically appended at the end of the content stored in s.
    This explains a lot.

    The first paragraph explains how get() reads into the string s. If your reading doesn't go as planned and there is more than n - 1 bytes to read, then one part would be stored in s and another part (possibly the rest) stored in s2 in another call.

    Or, your program should work perfectly because there is not that much data. In that case, the second paragraph explains what happens to the delimiting (separator) character '\n', and that is probably why it does not work. The next get() call or whatever would pick up the '\n' and return successfully. If it is that, the manual explains that you could use getline() instead.

    There is the off chance that a stream error happened and things aren't working because you didn't handle that case.
    Last edited by whiteflags; 10-02-2010 at 10:56 PM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You really should change
    cin.getline(address,90);
    to
    std::getline(address, std::cin);
    Where address is a std::string. That way, you avoid stuff being left in the input buffer (hopefully), buffer overruns, and char arrays.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 05:32 PM
  2. cin.get();
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2005, 07:51 AM
  3. curiosity about cin.get() and cin.getline()
    By ssjnamek in forum C++ Programming
    Replies: 18
    Last Post: 11-30-2003, 01:26 AM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM