cin '>>' question about buffer
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: cin '>>' question about buffer

  1. #1
    Join Date
    Mar 2009
    Posts
    82

    cin '>>' question about buffer

    Hello! I am new to this forum and I like C++ programming. But I have one question:
    If cin>> leaves '\n' in the buffer, why this:

    cout<<"Write your age: ";
    cin>>age;
    cout<<"Write whatever: ";
    cin>>whatever;
    cout<<"Write etc....";
    cin>>etc;

    works perfectly, without any errors?

    Thanks in advance.

    Regards.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,561

    Re: cin '>>' question about buffer

    because the ">>" ignores whitespaces ( '\n' is a whitespace ).

    So in your example, the

    Code:
    cin >> whatever;
    skips the '\n' and reads until the next whitespace.

  3. #3
    Join Date
    Mar 2009
    Posts
    82

    Re: cin '>>' question about buffer

    Yes, it skips the '\n' that's why it stays in the buffer.
    If I write:

    cin>>whatever;
    cout<<"Put something";
    cin.getline(something,60);

    It will skip cin.getline, since '\n' is already found in the buffer. That means that cin>> lets the '\n' in the buffer.

    Does cin>> stops reading and extracting when '\n' is found in the buffer?

  4. #4
    Join Date
    Dec 2008
    Posts
    56

    Re: cin '>>' question about buffer

    Yes, cin leaves the \n in the buffer when reading in a non-string value. You can flush that \n from cin by using the ignore() method. For example:

    Code:
    int age;
    char str[50];
    
    std::cout << "Enter your age: ";
    std::cin  >> age;
    std::cin.ignore(100, '\n');       // ignore the next 100 characters up to and including the \n
    
    std::cout << "Enter a string: "
    std::cin.getline(str, sizeof(str) - 1);
    Now, since you are programming in C++, I cannot think of any rational reason for declaring an array of characters to represent your strings. It's appropriate for C programming, but in C++, there is the std::string class. So if you take the code above, and modify it slightly, the ignore() becomes moot.

    Code:
    int age;
    std::string str;
    
    std::cout << "Enter your age: ";
    std::cin  >> age;
    
    std::cout << "Enter a string: "
    getline(std::cin, str);
    Avoid using char arrays; use the std::string. It will make your programming a lot easier. If you need to pass the std::string as a const char* to a C-style function, then use the c_str() method afforded to std::string.

  5. #5
    Join Date
    Mar 2009
    Posts
    82

    Re: cin '>>' question about buffer

    Thanks for the posts. I was wondering why the code in the #1 post still works, with '\n' in the buffer. Does the 2nd cin>>whatever, doesn't read '\n' from the buffer?

  6. #6
    Join Date
    Dec 2008
    Posts
    56

    Re: cin '>>' question about buffer

    When I attempted to read a value, and then read two strings, using std::cin.getline(), the program never paused to prompt me for the first string. This tells me that a '\n' is still in the stream. Using getline() (available when including string), removed that issue.

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,561

    Re: cin '>>' question about buffer

    operator >> skips leading whitespaces

    getline() does not skip leading whitespaces

  8. #8
    Join Date
    Mar 2009
    Posts
    82

    Re: cin '>>' question about buffer

    cin.getline() extracts and discards '\n' from the buffer (as default delimiter). cin>> doesn't discard '\n' from the buffer (skips the '\n' and that's why it stays in the buffer). Probably the code in the first post works because the second cin>> doesn't read '\n' (that's already in the buffer) and continues forward. Can you please tell me what is the default delimiter for cin>> ?

    If I have one string, for ex. char str[50]; and use cin>>str; and insert My name (with blank space), why it doesn't work correctly? What happens with the buffer?

    Thank you.

  9. #9
    Join Date
    Dec 2008
    Posts
    56

    Re: cin '>>' question about buffer

    You ask a question, and funnily you answer it... but don't know it. As it has been stated earlier, the white-spaces (e.g. space, tab, '\n') are the delimiters.

    And as I mentioned earlier, you should not define a string as an array of characters. Use std::string instead. It has more safeguards and functionality than a basic array of characters.

  10. #10
    Join Date
    Mar 2009
    Posts
    82

    Re: cin '>>' question about buffer

    Hehe, I am just "supposing", since I don't know the answer. Ok, I would accept the advice and use strings in future. My conclusion is that the delimiter of the cin>> is white space. If the delimiter is '/n' then the code in the #1 will not work, right?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center