C++ tutorial for beginners: Lesson #1 2/2
  1. #1

    C++ tutorial for beginners: Lesson #1 2/2

    End from post: C++ tutorial for beginners: Lesson #1 1/2
    -----------------------------------------------------------------

    Step 7:

    Now go back and delete:

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

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

    [Code9

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    int main(int argc, char *argv[])
    {
    char szText[] = "The reason I'm doing this is.\nI'm testing out this line feed thing this crazy guy keeps ranting about.";
    cout << szText << '\n';
    system("PAUSE");
    return EXIT_SUCCESS;
    }
    [/code]
    You may now compile and run this again (f9)

    That's better right? I thought so, now I will explain why we have [] beyond szText when declaring szText we are using more than one character or 'char'. 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 'arrays'.


    Step 8:

    In this step you'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[] = "The reason I'm doing this is.\nI'm testing out this line feed thing this crazy guy keeps ranting about.";
    cout << szText << '\n';
    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'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[])
    {
      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'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's useful. [13] signifies that there are only 13 positions for 'char'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 '\0' 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[])
    {
    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'll use this in our next exercise we'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[])
    {
      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 '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
      '\0' 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.

    Credits for Kozical!
    Last edited by Smileman; 5th September 2007 at 08:23.

  2. #2
    //well done tutorial kozical :)


    http://kozical.com/default.aspx?&x=3&z=1


    This is a rip off and you call it "yours"..

    "Hello my name is Kozical. 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." - you just changed kozical to smileman!
    Last edited by TURiOL; 2nd September 2007 at 16:58.

  3. #3
    rofl busted. if you want to start coding your own hack google a few books like c++ for dummies. read them learn your basics. and then run through some tutorials like these. http://www.ultimategameprogramming.c...ory=Cpp&page=1
    they are very helpful and will get u on your way to coding your first hack. another helpful site is http://forums.gamedeception.net this is a dev forum where some of the best hack coders originate from to help you learn. but dont go there asking for code that will only get u flamed on.

  4. #4
    printf? Is this a C or C++ tutorial? Quick lesson for those interested in learning C++. printf is obsolete. It's cout now. Yes you can use it, but its not the norm for a C++ application.

  5. #5
    Quote Originally Posted by Strife View Post
    printf? Is this a C or C++ tutorial? Quick lesson for those interested in learning C++. printf is obsolete. It's cout now. Yes you can use it, but its not the norm for a C++ application.

    printf is oldstyle but im using console output myself(cout).

  6. #6
    Quote Originally Posted by TURiOL View Post
    printf is oldstyle but im using console output myself(cout).
    It's not so much that its old school, but the fact that printf was developed for C. cout was developed for C++. So what I'm trying to say is, why teach printf in a C++ tutorial? You don't.

Tags for this Thread

Posting Permissions

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

Log in

Log in