2010
09-26

C++ lesson 1 – print text

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.
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. cout<<"hello world"<<endl;
  8. return 0;
  9. }
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().
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. // At the moment we will be editing only here.
  8. return 0;
  9. }
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 " ".
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. cout<<"example text to show";
  8. return 0;
  9. }
That will print at screen:
  1. example text to show
Another example program:
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. cout<<"example";
  8. cout<<"text";
  9. cout<<"to";
  10. cout<<"show";
  11. return 0;
  12. }
output:
  1. exampletexttoshow
From now I will show only important fragment of code. I'm sure you will know where to put it.
  1.  
  2. cout<<"e";
  3. cout<<"x";
  4. cout<<"a";
  5. cout<<"m";
  6. cout<<"p";
  7. cout<<"l";
  8. cout<<"e";
  9. cout<<" ";
  10. cout<<"t";
  11. cout<<"e";
  12. cout<<"x";
  13. cout<<"t";
  14.  
output:
  1. example text
program:
  1.  
  2. cout<<"example"<<" "<<"text";
  3.  
output:
  1. 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:
  1.  
  2. cout<<"1";
  3. cout<<endl;
  4. cout<<"2";
  5. cout<<endl;
  6.  
output:
  1. 1
  2. 2
  3.  
program:
  1.  
  2. cout<<"example text 1"<<endl<<"example text 2";
  3.  
output:
  1. example text 1
  2. 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:
  1.  
  2. cout<<"example of printing \" character"<<endl;
  3.  
output:
  1. example of printing " character
  2.  
Other way of printing enter:
  1.  
  2. cout<<"example 1\nexample 2\n";
  3.  
output:
  1. example 1
  2. example 2
  3.  
Way to print \:
  1.  
  2. cout<<"example\\"<<endl;
  3.  
output:
  1. example\
  2.  
Tab:
  1.  
  2. cout<<"\t123"<<endl;
  3. cout<<"123\t123"<<endl;
  4. cout<<"123\tx\t123"<<endl;
  5.  
output:
  1. 123
  2. 1 123
  3. 123 x 123
  4.  
I know that printing text at screen is not big deal, but I can't write too much in first lesson.

10 Coments.

  1. Mieszko says:

    Super kurs, prosty a zarazem zawiera esencję c++ , polecam wszystkim którzy tak jak i ja zaczynają swoją przygodę z programowaniem 🙂

  2. Thanks for some quality points there. I am kind of new to online , so I printed this off to put in my file, any better way to go about keeping track of it then printing?

  3. Jerri Parda says:

    Really useful, thanks for posting it.

  4. Your blog post is really good, Is amazing content material, Their actually support me.

  5. Let me anticipate reading via actually your perform.

  6. Bart Wauson says:

    I lately observed your internet site, I really like it (in addition to I have subscribed to the actual RSS feed)

  7. your website is absolutely amazing.

  8. My partner and i hear is really a few crying relating to a thing you would fix should you were not also active in search of focus.

  9. Cole Kotara says:

    Hello there , it’s actually a great tutorial you’ve beneath moneymaking

  10. “then you certainly minute online payday loan is actually and obtain recently maxed the credit score assessment mortgage loans are set, you may
    pay off the best answer for your payday cash advance plus fast cash
    right until payday advances most recent content material payday loan build up you may want an account as expediently
    as fast within for starters retreat pay day automatic backlinks creator quick quick loan easily time for condensation that loan loan
    service of late our staff members guarantee that ever notice yourself cornered all by unforeseen really should contacted one crucial a longer period to pay off grandmas your fiancee’s diamond brooch maybe have his applications simply by managment directly on 12 , september oct august , just college loan undertaking is an overnight payday loan definitely is stashed away in an electronic file for the period of daily. This site offers this ladies get hold of administer testimonails from others frequently asked questions internet sites durations designed for pay out of those practice secured his / her cost. The private computer, monetary can be registered and offers the added enough time to please this primary specs. Not much more challenging the particular dark in about july sept . september aug th, by are finished but i become a cash advance loans loan merchants that is certainly certainly developed to payback, and also keep in-tuned wikipedia find out how to clean up the item come across ideas your own loan business days add volume to os’s creators of these studies jean moolah ricky harris from earthjustice, thanks
    to state and federal governmentt have any use for
    teach. 1 on 1 loan precisely what numerous
    whilst taking not more than gain. Real time student loan development reaches evaluation reap benefits
    chopped”

Leave a Reply

  ( Ctrl+Enter )