GoldenGaming

Welcome to GoldenGaming. We appreciate your support and hope that you be an active member. Don't Forget To Sign Up!

Welcome to Golden Gaming
Contact an Admin or Moderator for any issues you have
We Are Purchasing A Domain After 100 Members!
You can now use symbols in your username, just don't forget them!

    The Basics of C++

    Share
    avatar
    I Am Programmer
    Rookie User
    Rookie User

    Posts : 20

    The Basics of C++

    Post  I Am Programmer on Wed Dec 08, 2010 7:21 pm

    I think the best way to learn how to use c++ code is to start off with the most basic program. For any one who has learned c++ they all started off with this code.


    #include
    int main (void)
    {
    cout << "Hello World!";
    return 0;
    }


    Now lets break the code down.

    #include

    At the very begining of EVERY C++ program you will need to put this. Basicly this tells the compiler to include the iostream.h header file. With out this you will not be going any where.


    int main (void)

    Int main void is the main driver in the program. You put all your code in here. Think of the int main (void) as the top of a box, and you put your code in the box.


    {
    This is used to create loops, and close the program. You need to always put a { after int main (void). You write your code between the { and closing }

    cout << "Hello World";

    Cout<< means to print out on screen. You then put " and fill in your text here then close" The ; is like a period at the end of the sentence. You always put the ; at the end of code, that is not dealing with functions.

    return 0;
    Return 0 is the end of your program. Like the conclusion of a essay its basically ending your code. Then you finish it off with the closing }.

    COMMENT LINES.
    If you want to write a side none in your program so you can remember what certain things are doing, you can put two // and then type what ever you want, and it will not be included in your program. The compiler doesn't do anything with it, pretty much its not there.


    Now lets kick it a step up.

    We will now learn how to add multiply subtract and devide things in c++.
    you will start off your program like any other using the header file


    #include
    int main (void){

    now you can add some things that are called variables. Variables store information. Names, numbers symbols. Anything.

    To declare a variable use either int, float, or char. We will start of with the basics for now.


    int is a number that is not a decimal.
    Floats can be a number like 1.5643234
    Chars are names. When using a char put ex. [20] at the end of the name so you can decide how much space you want. the 20 can be any number. Just make sure its a big enough number so you can have enough room to store your stuff.


    ok now you will learn how to use variables.

    #include
    int main (void)
    {
    int x=0;

    you tell what your using (int,float, or char) and then give it a name. We will use x for now. Also set it = to something or the compiler will fill the variable with junk.

    If you use more than one variable you can add them like so.


    #include
    int main (void)
    {
    int x=0,y=0,total=0;
    cout<<"Enter the number for x";
    cin>>x;
    cin>> is setting the variable.
    cout<<"enter a number for y";
    cin>>y;

    Now to add the numbers together. All you do is after the cin>>y;
    total=x+y;


    Its as easy ad that. If you want to subtract put
    total=x-y;

    To multiply we use * not x.
    total=x*y;

    And to devide we use /.
    total=x/y;

    to print variables out you put
    cout<

    if you want to put text then the variable put
    cout<<"Your x variable is"<

    If you want to print out all variables put
    cout<<<

      Current date/time is Sat Sep 23, 2017 5:44 pm