What's the difference between cin >> and cin.get()?

This is a discussion on What's the difference between cin >> and cin.get()? within the C++ Programming forums, part of the General Programming Boards category; Why does the following code not work? Code: char aName[20]; int number; cout << "Please enter an integer: "; cin ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    116

    What's the difference between cin >> and cin.get()?

    Why does the following code not work?
    Code:
        char aName[20];
        int number;
        cout << "Please enter an integer: ";
        cin >> number;
         cout << "Enter a sentence: " << endl;
         cin.get(aName, 20, '\n');

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,554
    You might want to read
    skipws - C++ Reference
    http://www.cplusplus.com/reference/i...nipulators/ws/

    I would guess you are not skipping past the newline on second input.
    I am a OK to good C programming; but, just starting to learn C++.

    Tim S.
    Last edited by stahta01; 11-23-2010 at 01:35 PM.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    After you enter your integer, there will be a newline in the input buffer that will be consumed by cin.get, I presume.
    Also, I would change

    char aName[20];
    cin.get(aName, 20, '\n');
    to
    std::string aName;
    std::getline(std::cin, aName);
    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.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    116
    Got it thanks.

  5. #5
    m&m
    m&m is offline
    Registered User
    Join Date
    Jun 2010
    Posts
    10
    You are entering a character after having just entered a number.

    cin.ignore(20,'\n'); should work.

    You ignore 20 non newline characters or the newline character (whichever comes first) in the buffer so that the cin for the second prompt will work properly.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    Why worry about the next 20 characters when you could just call ignore()?

  7. #7
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    116
    Quote Originally Posted by whiteflags View Post
    Why worry about the next 20 characters when you could just call ignore()?
    For some reason it only takes up to 9 characters. So for example if I inputed "abcdefghijkl" it only gives me "abcdefghi"

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char sentence[20];
        int num;
        cout << "Enter a number: ";
        cin >> num;
        cout << "Enter a sentence: ";
        cin.ignore(20, '\n');
        cin.get(sentence, '\n');
        cout << "num is: " << num << "\nsentence is: " << sentence << endl;
    
        return 0;
    }
    Last edited by c_weed; 11-24-2010 at 05:45 PM. Reason: added example

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    Well \n is a number too

    cout << int('\n') - 1 << endl;

    Remember how

    cin.get(sentence, '\n');

    works? You asked for the first nine bytes and you got it.

    I said why not ignore() in reference to what m&m posted because in this case, the 20 is just a magic number that contributes precisely zero in any situation.
    Last edited by whiteflags; 11-24-2010 at 06:55 PM.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    116
    I'm slowly learning

    Something still confusing me is how come sentence holds a max of 19 characters?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        //cout << int('\n') - 1 << endl;
        char sentence[20];
        int num;
        cout << "Enter a number: ";
        cin >> num;
        cout << "Enter a sentence: ";
        cin.ignore();
        cin.get(sentence, 20);
        cout << "num is: " << num << "\nsentence is: " << sentence << endl;
        cout << "19th element: " << sentence[19];
    
        return 0;
    }
    Is it because the null character is at sentence[19] and cout can never print the null character?

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    The sentence variable holds a maximum of twenty characters. If we look at it in math terms, a set from zero to nineteen inclusive has twenty elements. A C string is defined as a sequence of characters terminated by zero. So, sentence holds nineteen characters because the last character must be a zero, but you could always store a shorter string in sentence. I hope that is clear. There is no reason why cout cannot print a zero.

    Also, this means that all previous examples of get() in this thread have terrible bugs.
    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.
    get - C++ Reference

    At a minimum then:
    Code:
    #include <iostream>
    int main() 
    {
    	char sentence[20];
    	std::cout << "enter string:\n";
    	std::cin.get(sentence,  sizeof(sentence) - 1);
    	std::cout << sentence << '\n';
    }
    But the string class has already been mentioned, and there is no reason not to use it right now.
    Last edited by whiteflags; 11-25-2010 at 07:52 PM.

  11. #11
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    116
    Why doesn't this output the null character '\n'? I mean '\0'
    Code:
    char sentence[] = "abcde";
        cout << sentence[5];
    I thought when the c-string is created it automatically adds a null character, after the last character, to signify the end.
    Last edited by c_weed; 11-26-2010 at 02:05 PM.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Since when is '\n' a null character? The null character, '\0', is not actually part of the string. It signals the end of the string. It is never actually outputted; thus you will never see it.
    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.

  13. #13
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    116
    Sorry I meant '\0' not '\n'. I thought I was getting ripped of the last character for the '\0'? For example
    Code:
    char sentence[20];
    cin.get(sentence, 20);
    will only let me store 19 characters in sentence. I thought that's because the last element (sentence[19]) is used for the null character ('\0')?

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Yes, of course it is, since you're using a C-style string.
    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.

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    Quote Originally Posted by c_weed View Post
    Sorry I meant '\0' not '\n'. I thought I was getting ripped of the last character for the '\0'? For example
    Code:
    char sentence[20];
    cin.get(sentence, 20);
    will only let me store 19 characters in sentence. I thought that's because the last element (sentence[19]) is used for the null character ('\0')?
    Actually it will let you store 20 characters in a 21 character string. I already posted a blurb on how this works.

    cin.get(sentence, 19);

    Means that a maximum of 18 characters will be written to string and a zero automatically placed at the 19th place, if you use up the whole string. Please pay attention to the small stuff.

    I still recommend std::string.

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

Similar Threads

  1. cin.get() Troubles
    By yukapuka in forum C++ Programming
    Replies: 19
    Last Post: 06-04-2008, 01:30 PM
  2. Istream cin vs. cin.get
    By Mehdi in forum C++ Programming
    Replies: 11
    Last Post: 08-31-2006, 11:26 AM
  3. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 05:32 PM
  4. Confused about cin.get(); and classes.
    By RaccoonKing in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2005, 11:44 AM
  5. cin.get();
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2005, 07:51 AM

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