Screen Operator in C ++
Let's look at some of the features of the cin
output statement.
1) You can write multiple output statements on the same line.
For example, a sequence of statements
cout << "текст1";
cout << "текст2";
can be written on one line
cout << "текст1" << "текст2";
In any case, the phrase
text1 and
text2 will be displayed on the same line
2)To transfer text to a new line, you can use the sequence of characters "
\n
", or the
endl
command;
The next two lines are identical in result. You can use any method
cout << "текст1 \n" << "текст2"; //обратите внимание "\n" записывается в кавычках
or
cout << "текст1 <<endl<< "текст2";
In the task, you will have to work with the source code of the program and force the computer to display certain information in the required form.
***Advanced material***
This material is intended for those wishing to learn the classical C language, and its differences from C ++
Knowledge of this material will help you in solving the olympiad problems
f you want to make the program faster (for example, when solving olympiad problems), then you can use the format output operator
In general, the formatted output to the screen is as follows:
printf("<format string>",<comma-separated variable names>);
//to work, you need to connect the
stdio.h
library
We will deal with variables in subsequent courses. Output variables is not always needed.
format string
- this is a line that, in addition to text, can also contain special patterns, which we will also talk about in future courses.
In the general record, the characters
<>
are used to indicate that the information contained between them may be different, it all depends on the task. When recording a program, the
<>
characters are omitted.
If you write plain text inside a format string, it will be displayed on one line in the same way as it is written, on one line.
If we need to output something from a new line, then for this a special symbol
\n
is used in the place where the transition to a new line is planned.
For example,
printf("Everybody \nloves \nkitten");
will output each word from a new line