-4
#import <iostream>

using namespace std;

int main()
{
cout << 123 <<'\n';
cout <<"$100 recived \n";
cout <<"see you tomorrow \n";

return 0;
}

in this C++ program line, i dunno the function of the '<<' here; (i'm a newbie)

for example, an error occurs when i remove the '<<'in the first line

#import <iostream>

using namespace std;

int main()
{
**cout << 123 '\n';**
cout <<"$100 recived \n";
cout <<"see you tomorrow \n";

return 0;
}

i dunno why I need the '<<' please help me :(

K.Choi
  • 13
  • 1
  • This is one of those things that you begin to understand once you get more fluent in the language. C++ has a steep learning curve like that. Until you reach operator overloading, don't bother understanding this... – DeiDei Apr 21 '18 at 05:34
  • 1
    And why are you using `import` (gcc deprecated extension)? – user202729 Apr 21 '18 at 05:34

2 Answers2

1

The << symbol is an ostream operator :

This operator (<<) applied to an output stream is known as insertion operator.

In this case you can think of << as a way of converting a type to something cout understands.

First << '\n' converts the character \n to data that an output stream understands. << 123 does the same for the int 123.

Together << 123 << '\n' combines each stream into one to be passed to cout.

Finally, each different type needs its own << operator to be converted to a stream type for cout. When you try and run << 123 '\n' the compiler does not know how to convert an int and a character to a stream at the same time.

Increasingly Idiotic
  • 5,700
  • 5
  • 35
  • 73
0

In C++ << this is output operator. and >> this is input operator. So while using << it expected something to be shown in output.

So in your firstline cout << 123 <<'\n'; second << output operator expected something for displayed, But found nothing, that's why it gives you ERROR.

Mehadi Hassan
  • 1,160
  • 1
  • 13
  • 33
  • 2
    It's not called like that. `<<` is either shift left or insertion. – user202729 Apr 21 '18 at 05:43
  • I am talking about the symbol that C++ uses for its output and input stream. @user202729 – Mehadi Hassan Apr 21 '18 at 05:51
  • Wrong terms, wrong concepts. –  Apr 21 '18 at 06:06
  • 1
    @NickyC then would you please tell what is **Right terms, right concepts.** – Mehadi Hassan Apr 21 '18 at 06:07
  • 1
    I don't need to. The right term is told to you right in the very first comment. The right concepts are explained in [how does cout << actually work?](https://stackoverflow.com/a/5508865/2486888), which this question is a duplicate of. –  Apr 21 '18 at 06:15