Min menu

Pages

VB

Explanation of the print command cout

Explanation of the print commandcout

In general, we use the command coutto print whatever we want in the console.
When using the command coutyou must put the factor <<before anything you want to pass it to so that it is printed.


Example

main.cpp
                    #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 package iostreamcontaining the basic commands we need in applications that areConsole ApplicationWhich we will use in most programs such as the print command coutand other ready-made commands and functions that will pass with you later in the course.

  • The reason to put std::before coutis that the command coutis in it.

  • The function main()that we defined starting from the third line must be of its type intbecause it represents the starting point of the program.

  • Putting it return 0;in the sixth line and as the last command in the function main()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 coutin . commandC++

To combine several different things and display them using the command coutwe put the symbol <<between each two things we want to print.


In the following example, we create a program with two variables nameandyear.
Then we presented them as one sentence.

Example

main.cpp
                    #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, coutyou 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

main.cpp
                    #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 endlis that the agent endlis in it.

The importance of the word usinginC++

In the beginning, we will explain the word usingin boring detail in a later lesson due to its importance in projects.
Now, if you want to write the two commands coutdirectly endlwithout having to write before themstd::,Include the domain stdonce in the file and you are then able to directly use whatever is in it.

To include the range stdin the file main.cppwe write using namespace std;over the functionmain().


Now, we will repeat the same example as before with the scope included stddirectly.

Example

main.cpp
                    #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 \ninC++

If you want to go down on a new line without typing, << endlyou can pass \nas text.
Note: Most programmers prefer passing \nrather than writing << endlbecause it is more concise.

Information: \n an abbreviation for a sentenceNew Line.


In the following example we pass as the \ncommand text in order coutto go down on a new line.

Example

main.cpp
                    #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 coutthis 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

main.cpp
                    #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 \tinC++

If you want to put several blank spaces at once and in an orderly manner, it is better to use the symbol \tinstead of putting the spaces yourself.
One of the things that might make you use \tit 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 \tbetween the words in order to put an automatic space between them in an orderly manner.

Example

main.cpp
                    #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 \ainC++

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 \acommand textcout.

Information: \a an abbreviation of the wordAlert.


In the following example we used the code \ato trigger a beep in the user's device.

Example

main.cpp
                    #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

main.cpp
                    #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