Introduction to C++

Discussion in 'Programming General' started by Visual Basic Matt, Jan 15, 2009.

Introduction to C++
  1. Unread #1 - Jan 15, 2009 at 4:23 PM
  2. Visual Basic Matt
    Joined:
    Jan 29, 2008
    Posts:
    647
    Referrals:
    2
    Sythe Gold:
    56
    Discord Unique ID:
    223154494878253056

    Visual Basic Matt Apprentice

    Introduction to C++

    First you'll need a compiler. I personally use Dev-C++ because it seems to work fine.

    Download Dev-C++.
    [​IMG]

    Select your language, hmm...
    [​IMG]

    Finish Installation.
    [​IMG]

    Select your theme, I personally think this to be pretty neat.
    [​IMG]

    Wait as it configures/parses all the files.
    [​IMG]

    Be patient as it loads up for the first time.
    [​IMG]

    You always will get these neat little tips, I suggest you read them as they are a great help on getting to understand the program.
    [​IMG]

    Now it is officially installed and ready to use.

    First to start out you need to open a new source file.
    You can do this by going to file/New/Source File, or by simply pressing Ctl+N.

    You will see a blank screen where your code will go.
    Starting out you must remember one thing, in all of your programs you must put the following inclusion at the top of all your code, which is where you will be including other functions aswell.
    Code:
    #include <iostream>
    Your screen should look like this.
    [​IMG]

    When you include something like that, for instance, lets say you wanted the program to use mathematical methods without having to create your own. You can import cmath, which would look like this.
    Code:
    #include <cmath>
    You would just have to add that to the top of your code and this would make it very easier to do things without creating your own functions. This is just one of the many functions you can include to your project. Although you Must always include the iostream in all of your projects.

    Next you will start off with the main function of your program. This will be triggered the second the user opens the application.
    Code:
    int main()
    {
    
    
    
    }
    That is the main function that should always be the first line of code in your program after all of the includes and your variable declaration. Your code will lie in between the brackets "{" and "}".

    Inside the brackets you should start out with the following code.
    Code:
    using namespace std;
    This will allow the program to communicate with the user.

    First off, you will need to declare your variable.
    Code:
    int x;
    This will tell C++ that x is your integer. You can now make the variable "x", any value you wish.

    There is two main communications with C++ and the user. They would be "Cin" and "Cout". You may ask. What does that mean? Well, The "C" stands for console because we are making a console application. In is to ask the user for a variable of some sort. Out is to output the info to the user. So lets say we wanted to ask the user what their favorite number was. We would use the "Cout" command to ask the user the question.
    Code:
    cout << "What is your favorite number:";
    So now the user knows that he/she needs to input here favorite number, but the application must be able to read what he/she inputs. That is why we have the "Cin" command.
    Code:
    cin >> x;
    You will notice that there is a ";" after all of the lines of code here, well this is what tells C++ that it is done at that line of code, and doesn't need to be continued. But be careful, during if commands and many other commands you may do, the line isn't finished doing its job yet, therefore it needs to continue on until its done.

    Well, now we told the program to read what the user put in, but what will we do with that number? Well, we can do a million things with a number, but just for now, lets have the program tell the user the number. We will do this with the "Cout" command to display it to the user.
    Code:
    cout << "Your favorite number is " << x << "." << endl;
    You should notice how their is "<" and ">" well, simply there opposite so when you do "Cin" and "Cout they will be opposites. Another thing is that anything in the """ will be recognized as text given to the user and without them, the program will know to go figure out what the variable "x" is, or whatever the variable is. One last thing you will see is "endl" That simply ends the line so the next piece of code will not be upon it.

    If you were to compile and run it, it would disapear really fast after the user inputs his/her favorite color. This is because we need to tell C++ to pause the program at its current line. You would just add this bit of code.
    Code:
    system("pause");
    Then finally you will need to have the program stop itself, or just finialize everything, just add the following to the bottom of your function.
    Code:
    return 0;
    Your code should finally look something like this.
    [​IMG]

    Now that your done test it out by going to Execute/Compile and run or simply press F9.

    It should end up looking like this.
    [​IMG]


    This was my first programming tut, I hope you enjoyed it. Please comment/rate and I'll try and make more. I know I'm visual basic matt but I don't just program in vb. :p Thanks!
     
  3. Unread #2 - Jan 15, 2009 at 5:11 PM
  4. r0ut3r
    Joined:
    Jan 2, 2009
    Posts:
    263
    Referrals:
    0
    Sythe Gold:
    0

    r0ut3r Forum Addict
    Banned

    Introduction to C++

    Leeched tut, Same one is on cs.washington. Good effort in adding some word's "Select your language" "Hmm..."
     
  5. Unread #3 - Jan 15, 2009 at 6:21 PM
  6. Visual Basic Matt
    Joined:
    Jan 29, 2008
    Posts:
    647
    Referrals:
    2
    Sythe Gold:
    56
    Discord Unique ID:
    223154494878253056

    Visual Basic Matt Apprentice

    Introduction to C++

    This tut is not leached! I made it 100% Don't give me any of that crap... Pics and all words straight from my brain and screen cap program.
     
  7. Unread #4 - Jan 16, 2009 at 12:10 AM
  8. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Introduction to C++

    Please remove this, completely untrue...

    It should appear outside of your main function below the inclues...

    You can now make "x" any numerical value that doesn't contain a fraction and that is within the lowest possible value and max value an integer can contain.

    Use getchar() to pause the program and view output as its an actual standard and cross-platform. System("pause") only works on Windows and isn't defined on a majority of compilers.
     
  9. Unread #5 - Jan 16, 2009 at 1:29 AM
  10. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Introduction to C++

    I see what you did there...

    More importantly Matt, I'm not trying to be rude but learn what you're talking about before you talk about it.
     
< new runescape cheats how to dupe phats | ~~programing/scripting~~~ >

Users viewing this thread
1 guest


 
 
Adblock breaks this site