Module: (C++) Printing text to the screen


Problem

2/5

Two-line text output

Theory Click to read/hide

C++ screen output statement

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 << "line_of_text1";
cout << "line_of_text2";
can be written on one line
cout << "line_of_text1" << "line_of_text2";
In any case, the phrase line_of_text1 and line_of_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
#include <iostream>
using namespace stdl

int main()
{ 
    cout << "line_of_text1 \n" << "line_of_text2" << "\n";  //note the "\n" is written inside the inverted commas
    // or
    cout << "line_of_text2 "<< endl << "line_of_text1" << endl;
    return 0;
}


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.

If 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>);   

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\n");  

will output each word from a new line

 

Problem

Let's try to change the output statement on line 3 so that the text on the screen is displayed in the following format:
x y
5 y