ok, I am a new programmer with c++. I have been experimenting with cin, and cin.get and now I plan to learn cin.peek(). could someone please give me a little information on what it is and how it works, most sites don't really say much about it. All I know is that it takes the value of the next character.
Also, the program I was working on with cin.peek() is a validater for a float number. I want to use cin.peek to make sure the number is a float and doesn't have invalid characters in it. How would I do that with cin.peek?
Thank you very much in advance for your help. I have tried a few different things, but I really don't understand how cin.peek works, so I cant really program using it until I do.
It works exactly like get(), except it doesn't remove the character from the input stream.
So, if the stream contained the string "foobar", repeatedly calling get() would return 'f', 'o', 'o', 'b', 'a', and 'r'; but repeatedly calling peek() would return 'f', 'f', 'f', etc.
ok, that makes sense. So if I wanted to peek at the next character from an input stream, using your example foobar, repeatedly calling peek would just return f,f, f. So if I wanted to us peek to see the o, o, b, etc how would I do that?
You can't, AFAIK. Streams are queues. You can only look at what's at the top.
If you want to do that, you need to store characters on a stack as you get() them, and when you're done, you putback() them in reverse order.
With a stream containing "foobar" where you want to remove the 'b', you get() 4 times, and then putback() 'o', 'o', and 'f', resulting in "fooar".