What means:

  1. cin allows you to read data from a user terminal,

  2. cout allows you to output data to the user terminal.

There are 2 code fragments:

 #include <iostream> using namespace std; int main() { cin >> "Hello, world!" >> endl; return 0; } 

and

 #include <iostream> using namespace std; int main() { cout << "Hello, world!" << endl; return 0; } 

Yet I did not understand their difference, both in the terminal display the text Hello, world!

  • Personally, I have a compilation error - Herrgott
  • Complement code (library + namespace) - Ilnyr
  • so I did just that - Herrgott

1 answer 1

The code in the example is incorrect. I wrote the following:

  1.cc:7:12: note: cannot convert '"Hello, world!"' (type 'const char [14]') to type 'unsigned char 

And it is quite logical, since in C ++, string literals are constants, i.e. they cannot be changed, and the std::cin operator >> changes its first argument. You can look at the signature of std::istream::operator>> (and cin is of type istream ) in the standard library and const is not there ... Ie in fact, there is no such function that suits you. You can theoretically conjure it by rebooting, but is it necessary?

Moreover, cin>>"abc" and cannot display the string abc on the screen, since he has all the overloaded operator options >> do only data entry.

Conventionally, I saved your code as follows:

 #include <iostream> using namespace std; istream& operator>> (istream& a , const char *val) { std::cout<<val<<std::endl; return a; } int main() { cin >> "Hello, world!"; return 0; } 

Those. stupidly overloaded the >> operator for const char *.