-
March 7th, 2009, 03:24 AM
#1
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.
-
March 7th, 2009, 04:10 AM
#2
Re: cin '>>' question about buffer
because the ">>" ignores whitespaces ( '\n' is a whitespace ).
So in your example, the
skips the '\n' and reads until the next whitespace.
-
March 7th, 2009, 04:50 AM
#3
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?
-
March 7th, 2009, 07:15 AM
#4
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.
-
March 7th, 2009, 07:35 AM
#5
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?
-
March 7th, 2009, 07:41 AM
#6
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.
-
March 7th, 2009, 09:15 AM
#7
Re: cin '>>' question about buffer
operator >> skips leading whitespaces
getline() does not skip leading whitespaces
-
March 7th, 2009, 11:58 AM
#8
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.
-
March 7th, 2009, 12:09 PM
#9
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.
-
March 7th, 2009, 04:26 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|