Explanation of the print commandcout
In general, we use the command cout
to print whatever we want in the console.
When using the command cout
you must put the factor <<
before anything you want to pass it to so that it is printed.
Example
#include <iostream> int main() { std::cout << "alawiri.com"; return 0; }
• We will get the following result when running.
alawiri.com
Explanation of the code
The command
#include <iostream>
in the first line includes the packageiostream
containing the basic commands we need in applications that areConsole ApplicationWhich we will use in most programs such as the print commandcout
and other ready-made commands and functions that will pass with you later in the course.The reason to put
std::
beforecout
is that the commandcout
is in it.The function
main()
that we defined starting from the third line must be of its typeint
because it represents the starting point of the program.Putting it
return 0;
in the sixth line and as the last command in the functionmain()
is a common thing, and its idea is to send the number 0 to the operating system when the program is finished or closed normally. This is how the operating system will be able to differentiate between a normal program termination and an abrupt termination(Crash)that occurs as a result of an error.
Basic Principles of Typography in C++
You must observe the following principles when using the commandcout
.
To display a number, put it as is.
To display the value of a variable, set it as is.
To display a character it must be placed between
' '
.To display a word or text, it must be placed between
" "
.
Print multiple things at once with the cout
in . commandC++
To combine several different things and display them using the command cout
we put the symbol <<
between each two things we want to print.
In the following example, we create a program with two variables name
andyear
.
Then we presented them as one sentence.
Example
#include <iostream> int main() { char *name = "Ali"; int year = 1996; std::cout << "Hello my name is " << name << ", I born in " << year; return 0; }
• We will get the following result when running.
Hello my name is Ali, I born in 1996
Display content on a new lineC++
When displaying the content with the command, cout
you can specify the end of the line by setting the operator endl
, and then anything you type after it will appear on a new line.
Information: endl
an abbreviation for a sentenceEnd Line.
Example
#include <iostream> int main() { std::cout << "alawiri.com" << std::endl << "C++ course"; return 0; }
• We will get the following result when running.
alawiri.com C++ course
The reason for putting std::
before the agent endl
is that the agent endl
is in it.
The importance of the word using
inC++
In the beginning, we will explain the word using
in boring detail in a later lesson due to its importance in projects.
Now, if you want to write the two commands cout
directly endl
without having to write before themstd::
,Include the domain std
once in the file and you are then able to directly use whatever is in it.
To include the range std
in the file main.cpp
we write using namespace std;
over the functionmain()
.
Now, we will repeat the same example as before with the scope included std
directly.
Example
#include <iostream> using namespace std; // std Here we have included the scope int main() { cout << "alawiri.com" << endl << "C++ course"; return 0; }
• We will get the following result when running.
alawiri.com C++ course
Descending on a new line with the symbol \n
inC++
If you want to go down on a new line without typing, << endl
you can pass \n
as text.
Note: Most programmers prefer passing \n
rather than writing << endl
because it is more concise.
Information: \n
an abbreviation for a sentenceNew Line.
In the following example we pass as the \n
command text in order cout
to go down on a new line.
Example
#include <iostream> using namespace std; int main() { cout << "alawiri.com\nC++ course"; return 0; }
• We will get the following result when running.
alawiri.com C++ course
Here we have completely repeated the previous example with two commands cout
this time.
Note: This method is easier in terms of reading and modification, and for this reason we recommend adopting it instead of the previous method.
Example
#include <iostream> using namespace std; int main() { cout << "alawiri.com\n";
cout << "C++ course"; return 0; }
• We will get the following result when running.
alawiri.com C++ course
Put multiple blank spaces by the symbol \t
inC++
If you want to put several blank spaces at once and in an orderly manner, it is better to use the symbol \t
instead of putting the spaces yourself.
One of the things that might make you use \t
it is that when you print multiple lines it tries to display them to you symmetrically to make the content easier to read.
Information: \t
an abbreviation for a sentenceTab Space.
In the following example, we have placed the symbol \t
between the words in order to put an automatic space between them in an orderly manner.
Example
#include <iostream> using namespace std; int main() { cout << "Name:\tAli Rabeei\n"; cout << "Job:\tProgrammer"; return 0; }
• We will get the following result when running.
Name: Ali Rabeei Job: Programmer
Beep with the symbol \a
inC++
If you want to sound an alert to make the user pay attention to the console, you can do so easily by passing the icon as the \a
command textcout
.
Information: \a
an abbreviation of the wordAlert.
In the following example we used the code \a
to trigger a beep in the user's device.
Example
#include <iostream> using namespace std; int main() { cout << "Yes, you hear the alert :)\a"; return 0; }
• Do not forget to raise the volume of the computer until you hear the alarm sound when it is turned on, and the following text will appear to you as well.
Yes, you hear the alert :)
Blank spaces and code order inC++
languageC++Not affected by white spaces(White Spaces)Such as spaces and blank lines, so that some programs can be written entirely in one line, but we do not recommend that at all. It is preferable to leave spaces and blank lines so that you are able to review and understand the code quite easily when referring to it.
In the following example, we have written the same as the previous example on one line.
Caution: Although the following method does not have any programmatic problems, we advise you not to adopt this method at all when writing the code.
Example
#include <iostream> using namespace std; int main() {cout << "alawiri.com"<<endl<<"C++ course";return 0;}
• We will get the following result when running.
alawiri.com
C++ course