tutorial C++ part1
Basic lesson of programing in C++. I will show how to print text at screen.
Are you ready ?
First at all you need to make sure that hello world program is compiling.#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
cout<<"hello world"<<endl;
return 0;
}
If that do not work then go back to lesson 0.
Adding anytinig to hello world
I will explain later what #include mean, at the moment let say that it need to be in every program. Same with using namespace std; and int main().#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
// At the moment we will be editing only here.
return 0;
}
We will put there many stange commands. Every command need to be end with ;
Print text at screen
Printing text at screen command begins with cout. After that come << and that what you want to print at screen. In case of printing some text at screen you need to write that text between " ".#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
cout<<"example text to show";
return 0;
}
That will print at screen:
example text to showAnother example program:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
cout<<"example";
cout<<"text";
cout<<"to";
cout<<"show";
return 0;
}
output:
exampletexttoshowFrom now I will show only important fragment of code. I'm sure you will know where to put it.
cout<<"e";
cout<<"x";
cout<<"a";
cout<<"m";
cout<<"p";
cout<<"l";
cout<<"e";
cout<<" ";
cout<<"t";
cout<<"e";
cout<<"x";
cout<<"t";
output:
example textprogram:
cout<<"example"<<" "<<"text";
output:
example text
Printing Enter character
If some text is not between " " then it need to be some special text. For example it can be endl witch will work like enter on your keyboard. program:cout<<"1";
cout<<endl;
cout<<"2";
cout<<endl;
output:
1 2program:
cout<<"example text 1"<<endl<<"example text 2";
output:
example text 1 example text 2
Printing special characters
\ work differently than any other character if you put it between " ". It give special meaning to character after it. Way to print " character:cout<<"example of printing \" character"<<endl;
output:
example of printing " characterOther way of printing enter:
cout<<"example 1\nexample 2\n";
output:
example 1 example 2Way to print \:
cout<<"example\"<<endl;
output:
example\Tab:
cout<<"\t123"<<endl;
cout<<"123\t123"<<endl;
cout<<"123\tx\t123"<<endl;
output:
123 1 123 123 x 123I know that printing text at screen is not big deal, but I can't write too much in first lesson.