Page 1 of 4 123 ... LastLast
Results 1 to 10 of 32
  1. #1
    Senior Member
    Join Date
    Mar 2007
    Posts
    140

    C++ Tutorial For Beginners Lesson #1

    Hello my name is Bryan. I will be slowly walking you through this step by step and giving as much information as I recall it. I learned C++ a long time ago so bare with me. I will try my best to explain it thoroughly and also try to make my code simple enough for you to understand.

    Tools required:

    DevCpp (Free C++ compilation environment. I recommend it for beginners with no money) you can download that here http://www.bloodshed.net/devcpp.html
    One moderately intelligent fellow
    At least a few hours to spare.

    Alright we'll start small with a few examples of a console application.

    Firstly open up DevCpp after you have downloaded and installed it.

    ---


    Step 1: Open DevCpp then proceed to File (alt+f) -> New (alt+n) -> Project (alt+p)
    Step 2: At this point you will see a photo that resembles (VA-01)

    VA-01:


    Step 2 (continued): Select Console Application and then name your project Day1Project1

    Step 3: Save your .dev (project file) to a location in which you feel comfortable keeping your c++ files.

    Step 4: Right from the start DevCpp will equip you with a working application containing the code in VA-02 (below); at this point you can proceed to goto Execute (alt+x) -> Compile & Run (F9)
    (note: this will ask you to save main.cpp just save it wherever you saved your Day1Project1.dev (project file) )

    VA-02:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv&#91;])
    {
      system("PAUSE");
      return EXIT_SUCCESS;
    }
    Step 5: When this application runs it will appear blank and ask you to press any key to continue. This is a console application in action. However we want a more complex application so we will continue on. Now press a key to exit and go back to your code. I will go ahead and explain what each part does.

    -

    In C++ there are many types of data this you&#39;ll learn over and over again. All of which can be converted back to digits but we are nowhere near that far at this point. So I will explain some more about these &#39;types&#39;.

    In the code displayed you will see int and char. int as you may have guessed stands for integer integer is a number. So you may put only numbers into an integer variable. Example: int nTest = 1; You may safely say that nTest is equal to 1 now. Now char on the other hand stands for character which in this case we will only use ASCII characters. Example: char cTest = &#39;K&#39;; Now as you may have guessed cTest is equal to K. Notice I added &#39; and &#39; around my character this signifies to the compiler that this is a character and not an integer.
    In the coming exercises we will use these a lot but we still have not covered all the material given in your code. #include as you may have guessed includes another file to your current code. We will cover this more as we progress as of right now we&#39;re keeping this simple. int main(int argc, char *argv&#91;]) is the main function of your code anything done in this function will directly effect how your program works. That being said anything not done in this function will directly effect how your program works. If you add nothing to main &#39;s function then your program will do nothing.
    We will cover two more things and progress to our first exercise. using namespace std; we will cover this in other projects but I will say that the namespace keyword lets you partition an application into multiple subsystems. system("PAUSE"); this will stop your application from continuing and will send the message "Press any key to continue.." As you saw before when you compiled and ran the program.

    Step 6:

    In this step we will be working with the function cout which is basically an echo function for those of you who have used mIRC, php, perl, or any other coding/scripting languages. What this does it output characters to the console and that kind of works well with the name. There are several of these functions in C++ in which I will cover in this brief tutorial.

    Firstly, I want you to make room between { and system("PAUSE"); for you to work do not delete anything just press enter. (if you do delete something press ctrl+z)
    Now in this space you have just made type:

    Code:
    cout << "This is a test of the emergency broadcast system!";
    your code should now look like VA-03

    VA-03:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv&#91;])
    {
      cout << "This is a test of the emergency broadcast system!";
      system("PAUSE");
      return EXIT_SUCCESS;
    }
    your output should look like VA-04:

    VA-04:


    This looks wrong, right? The text runs into your "Press any key to continue" right? You have to add a new line or the technical term would be a line feed. C++ recognizes this as &#39;\n&#39;

    so lets try this again with a new string and a line feed.

    Step 7:

    Now go back and delete:

    Code:
    cout << "This is a test of the emergency broadcast system!";
    now we&#39;ll go back and add in

    Code:
    char szText&#91;] = "The reason I&#39;m doing this is.\nI&#39;m testing out this line feed thing this crazy guy keeps ranting about.";
    cout << szText << &#39;\n&#39;;
    Your code should now appear as follows:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv&#91;])
    {
    char szText&#91;] = "The reason I&#39;m doing this is.\nI&#39;m testing out this line feed thing this crazy guy keeps ranting about.";
    cout << szText << &#39;\n&#39;;
      system("PAUSE");
      return EXIT_SUCCESS;
    }
    You may now compile and run this again (f9)

    That&#39;s better right? I thought so, now I will explain why we have &#91;] beyond szText when declaring szText we are using more than one character or &#39;char&#39;. Also called an array of characters. This can also be done with integers in a little different way. We will do that later. Now I will show you a different way to echo to console. Also I will show you a little more about these &#39;arrays&#39;.


    Step 8:

    In this step you&#39;ll need to understand that a loop is something that continues until a certain condition is met. So say in this example: (this is not c++ code but rather my way of explaining how this step will work.

    While (I am Home)
    {
    keep lights on
    }
    turn lights off

    So it is safe to assume that when I am not home the lights will not be on. This is a basic "theoretical" example of a while loop.

    Now delete:
    Code:
    char szText&#91;] = "The reason I&#39;m doing this is.\nI&#39;m testing out this line feed thing this crazy guy keeps ranting about.";
    cout << szText << &#39;\n&#39;;
    from your code to prepare for the next step.

    Now add in:
    Code:
    int nCount = 10;
    
    printf("same as the function cout but sometimes better\n");
    
    //this is a single line comment
    
    /*
    this is a multiple line comment
    comments will not be seen by the compiler
    */
    
    while (nCount--)
    {
     printf("%d\n",nCount);
    
    }
    The -- following nCount means to decrease nCount by 1 this can also be expressed by nCount-=1
    Following that is the function printf . I will tell you some stuff about printf the printf family of functions are some of my favorite functions for console.

    The printf function is a echo function for console. I will print or echo whatever data you send it.



    Specifier Output Example


    cCharacterK


    dInteger123


    iInteger321


    eExponent5.3245e+2


    EExponent5.3245E+2


    fDecimal Floating point 5.3


    gUsed for the shorter of %e or %f 5.3


    GUsed for the shorter of %E or %f 5.3


    oSigned Octal720


    sString of charactersTesting this


    uUnsigned decimal number7777


    xUnsigned Hexadecimal numberb1


    XUnsigned Hexadecimal number (all caps)B1


    pPointer AddressB000:0000


    nNothing printed, must be a pointer to a signed integer, which holds the number of characters written so far.


    %%% will echo (print) out a %%



    That&#39;s as in-depth as we will go for right now. Back to our code. Your code should look like VA-05:

    VA-05:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv&#91;])
    {
      int nCount = 10;
    
    printf("same as the function cout but sometimes better\n"); 
    
    //this is a single line comment
    
    /*
    this is a multiple line comment
    comments will not be seen by the compiler
    */
    
    while (nCount--)
    {
     printf("%d\n",nCount);
    
    }
      system("PAUSE");
      return EXIT_SUCCESS;
    }
    Your output should be as shown in VA-06:

    VA-06:



    Step 9:

    Now that you&#39;ve experienced your first while loop. Lets try a for loop which is essentially the same but a little more productive and fun.

    Go back and delete:
    Code:
      int nCount = 10;
    
    printf("same as the function cout but sometimes better\n"); 
    
    //this is a single line comment
    
    /*
    this is a multiple line comment
    comments will not be seen by the compiler
    */
    
    while (nCount--)
    {
     printf("%d\n",nCount);
    
    }
    Then start typing this in. I suggest that you type and not copy/paste because you learn nothing that way.

    Code:
    char szText[13] = "Testing this";
    
    for(int nIncrement=0; szText[nIncrement]; nIncrement++)
    {
     printf("%c\n",szText[nIncrement]);
    }
    This is a bit different than the while loop but it&#39;s useful. [13] signifies that there are only 13 positions for &#39;char&#39;s in that array. In C++ 0 is your first position in the array instead of the usual 1-13 it is 0-12. Every string must be ended by a &#39;\0&#39; char which is a null character. Which is why you will often times hear them called null-terminated strings. We will experience this more in the next exercise.

    Your code should now display:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv&#91;])
    {
    char szText[13] = "Testing this";
    
    for(int nIncrement=0; szText[nIncrement]; nIncrement++)
    {
     printf("%c\n",szText[nIncrement]);
    }
      system("PAUSE");
      return EXIT_SUCCESS;
    }
    This is the output you should receive from this:

    DA-07:


    Too easy right? Okay now that was simple now lets try and understand ASCII better. Visit this link http://www.asciitable.com and view the character chart for a minute and have a gander at the integer references for each letter it represents. You want to be looking at the Dec number for the character by the way. Notice A-Z is respective to 65-90 and a-z is respective to 97-122. We&#39;ll use this in our next exercise we&#39;re gonna make a random string.

    Step 10: (Last step in this tutorial)

    Go ahead and delete your previous code as you have been doing.

    Code:
    char szText[13] = "Testing this";
    
    for(int nIncrement=0; szText[nIncrement]; nIncrement++)
    {
     printf("%c\n",szText[nIncrement]);
    }
    Now that this is our last step I will leave it a little more up to you. I explained each step inside the code so you can understand it as it works. The following code generates a random string 10 characters in length.

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv&#91;])
    {
      int nSeed = time(NULL);
      
      srand(nSeed);
      
      /*
       - srand() is a function to receive a seed or random number;
       - to use to conform another random number. That will be;
       - used when you use the function rand()
      */
      
      char *szRandomString = new char[11];
      /*
      this may look a little different but it is the same as;
      char szRandomString[11] what new char[11]; does is allocate;
      11 char &#39;s into an array. * in *szRandomString means that;
      this is a pointer to the head of an array or to a variable.
      */
      
      for(int nCount = 0; nCount < 10; nCount++)
      {
        if (rand() % 2)
        {
        /*
         - rand() is a function to retrieve a random number;
         - remember is a necessary to seed this in order to;
         - receive a truly random number;
         \
         - % is a modulus a mathmatical function used here to;
         - make our random output conform to a max of 2 and a;
         - minimum of 0
        */
        // if this is greater than 0 then we want it to be an uppercase letter
        szRandomString[nCount] = rand() % 25 + 65; // 65+25 = 90
        // we will receive a minimum of 65 and a maximum of 90 (A-Z)
        }
        else
        {
          // else the rand() % 1 was equal to zero so we want lowercase
          szRandomString[nCount] = rand() % 25 + 97; // 97+25 = 122
          // we will receive a minimum of 97 and a maximum of 122 (a-z)
        }
      }
      /* at this point nCount should be equal to 10 so we want to add our null
      since nCount is only declared inside the scope of our for loop we need 
      to set this manually or we could declare nCount outside of our for loop.
      */
      szRandomString[10] = 0; /* as you probably noticed on the chart
      &#39;\0&#39; or null is equal to zero so we can use the integer 0 to signify a null
      character.
      */
      
      // now we will print out our new random string.
      printf("%s\n",szRandomString);
      system("PAUSE");
      return EXIT_SUCCESS;
    }

    if you compile this, this should be your output:


    Keep in mind it is a random string so yours will not look like mine.

    ----


    That wraps up this, lesson #1, of C++ tutorial for beginners. Stay tuned for new lessons from myself. I will produce more when I get bored enough.


  2. #2

    Re: C++ Tutorial For Beginners Lesson #1

    Nice tutorial.

  3. #3
    Site Admin xHalt's Avatar
    Join Date
    May 2006
    Location
    Belgium
    Posts
    7,916

    Re: C++ Tutorial For Beginners Lesson #1

    Quote Originally Posted by Mchoodlum
    Nice tutorial.
    ?
    Nice work, though it lacks &#39;Hello World&#39;

    /stickied

  4. #4

    Re: C++ Tutorial For Beginners Lesson #1

    Hello World is overrated Very hawt tutorial, its sure to get quite a few traveling the coders path ^^


  5. #5
    Site Admin xHalt's Avatar
    Join Date
    May 2006
    Location
    Belgium
    Posts
    7,916

    Re: C++ Tutorial For Beginners Lesson #1

    Quote Originally Posted by null
    Hello World is overrated Very hawt tutorial, its sure to get quite a few traveling the coders path ^^
    Quiz: Guess the coding language 8)

    cout << "Hello world!";
    system.out.println ("Hello world!");
    10 PRINT "Hello world!"
    txt_msg.Text = "Hello world!"
    writeln (&#39;Hello world!&#39;
    <BODY>Hello world!</BODY>
    DISPLAY "Hello world!".
    print "Hello world!\n";
    echo &#39;Hello world!&#39;;

  6. #6

    Re: C++ Tutorial For Beginners Lesson #1

    Guess the coding language:

    cout << "My first C++!";
    system.out.println ("My first java!");
    10 PRINT "My first basic!"
    txt_msg.Text = "my first visualbasic!"
    writeln (&#39;my first DHTML / delphi&#39;
    <BODY>My first HTML!</BODY>
    DISPLAY "My first cobol!".
    print "My first python\n";
    echo &#39;My first php!&#39;;


  7. #7
    Site Admin xHalt's Avatar
    Join Date
    May 2006
    Location
    Belgium
    Posts
    7,916

    Re: C++ Tutorial For Beginners Lesson #1

    9/9...
    Cheers null!

  8. #8

    Re: C++ Tutorial For Beginners Lesson #1

    Not only that, but much easier then hello world !

    (WE get offtopic pretty easily)


  9. #9
    Site Admin xHalt's Avatar
    Join Date
    May 2006
    Location
    Belgium
    Posts
    7,916

    Re: C++ Tutorial For Beginners Lesson #1

    Quote Originally Posted by null
    (WE get offtopic pretty easily)
    We sure do...
    Shame on us! Let&#39;s mute each other as punishment

  10. #10

    Re: C++ Tutorial For Beginners Lesson #1

    WOW!
    This is the best C++ tut&#39; I&#39;ve ever seen!
    I&#39;ve been looking for ages to find a good-eas(y)(ier)-to-understand tutorial and I couldn&#39;t find one.
    And now I did.
    Thank you very-very much!

    w00t![br]Posted on: April 17, 2007, 11:31:28 PM_________________________________________________S till learning from this 2 tutorials.
    If I&#39;m getting a little bit more knowledge of the strings and their functions,... I&#39;ll tell you guys.
    Google is your friend
    Before asking any (stupid) questions use the search-option first
    Stupid question = stupid answer
    I do NOT respond to private messages when you ask for hacks.

Page 1 of 4 123 ... LastLast

Similar Threads

  1. Replies: 4
    Last Post: June 16th, 2011, 20:33
  2. C++ Tutorial For Beginners Lesson #2
    By Kozical in forum Basics
    Replies: 12
    Last Post: September 30th, 2009, 15:54
  3. [Help]I'm looking for tutorial...
    By SyncX in forum Basics
    Replies: 3
    Last Post: March 13th, 2009, 02:15
  4. Replies: 5
    Last Post: January 10th, 2008, 23:27
  5. [Tutorial] Basic Twirl Effect tutorial
    By JohnnyD in forum Tutorials (Art)
    Replies: 4
    Last Post: September 27th, 2007, 01:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •