Difference between these two

Started by
1 comment, last by programwizard 17 years, 10 months ago
What is the difference between the two code and which should I use?
#include <iostream>
using namespace std;
int main() {
     char c;
     while ( cin.get(c) ) cout<<c;
} 

And 

#include <iostream>
using namespace std;
int main() {
     char c;
     while ( cin.get(c) ) cout.put(c);
} 
Advertisement
[edit: whoops, not quite]Well, in this case they end up being exactly the same thing. In general, though, you should prefer the stream insertion operator (operator<<), as it performs formatted input (assuming this is what you want).


jfl.
From MSDN:

cout.put() is said to put a character in the stream, and the overloaded << operator writes to the stream. Basically, cout.put() can only write a single character the the console, while cout<< will write an entire string; if you pass a string to cout.put(), you will get a compiler error.

EDIT: Beaten to it...
------------------------------Support the Blue Skies in Games Campaign!A blog... of sorts.As a general rule, if you don't have a general rule in your signature, you aren't as awesome as someone who does. General rules roxor teh big one one ones.

This topic is closed to new replies.

Advertisement