Thread: Hello World!

Results 1 to 11 of 11
  1. #1
    merp.'s Avatar
    Join Date
    Aug 2012
    Gender
    male
    Posts
    147
    Reputation
    10
    Thanks
    301
    My Mood
    Bored

    Hello World!

    Hi guys, I'd like to help people out there who want to learn c++. There are probably tutorials that are better than mine, but I'm bored, and i'd like to use some of my time via teaching. NOTE: I'm still currently learning c++(can't self-study due to school), so i'll probably be able to only teach the basics until I have time to learn/not lazy to. I will try to release tutorials weekly, if possible.

    --------------------------------------

    Let's begin. First start off by downloading a C++ compiler. There are many out there: Codeblocks, Visual Studio, Turbo C++, etc. But I recommend using Visual Studio C++ 2010.
    NOTE: I recommend using Visual Studio C++ 2010!

    Download Link for Visual Studio:
    Visual C++ 2010 Express | Microsoft Visual Studio

    run the software and install it. (Sorry for not having any instructions about the installation process. :c)

    After installing it, open up Visual Studio C++ 2010.(If you're not using VC++2010, ignore and open your compiler)
    NOTE: You may be prompted to register for a key, just sign up and receive your key.

    After opening VC++2010, a 'start page' will appear.
    - Click on 'New Project'
    - Click on Win32 Console Application and give it a name. EG: Tutorial1
    - Click on Finish

    Now there should be some code on your page that looks like gibberish.
    Delete/erase this part off your code:
    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
    	return 0;
    }
    Now you should only have this: (only if you're using visual studio C++)
    Code:
    #include "stdafx.h"
    Don't worry if you don't understand what the heck these are, I will explain them soon.

    Write/copy+paste this down onto your compiler now:
    Code:
    #include <iostream>
    
    int main()
    {
    	std::cout << "Hello World!" << std::endl;
    	std::cin.get();
    
    	return 0;
    }
    Hmm. now what are these 2?
    Code:
    #include "stdafx.h"
    #include <iostream>
    '#include' <- Now this means what it means. you're including a certain file(s) for your c++ project.
    '#include "stdafx.h" <- This means to include the file "stdafx.h" into your project.
    '#include <iostream> <- This means to include the file <iostream> into your project.
    For now, this is all you need to know about these 2 files:
    stdafx.h is a precompiled header
    iostream is used so that we're able to use certain functions like 'cout' and 'cin', which i'll get to later.

    Code:
    int main()
    {
    	std::cout << "Hello World!" << std::endl;
    	std::cin.get();
    
    	return 0;
    }
    int main() <- int means integer, main() is a function. This is where the compiler starts executing the code.
    { <- this is an opening bracket. This is used to signify the beginning of code. if you don't use this, the compiler won't understand where the beginning of the code is at and will give you errors.
    } <- this is a closing bracket. Any code before this is executed by the compiler. Must be used with the opening bracket! without this, the compiler won't understand where the ending of the code is at and will give you errors.
    --------------------------------------------------
    std::cout << "Hello World!" << std::endl; <- Now what you might be thinking of is "oh, std, that stands for sexually transmitted disease. derp." well, sorry to burst your bubble, but it doesn't. std means standard.
    cout << "Hello World!" << std::endl; = basically means: "Print out to the screen whatever that is between the quotes." So in this case, it would print out to your screen (in a CMD console) "Hello World!"
    std::endl; = Now why is there a semicolon there? The reason there's a semicolon there is because you need one after each statement. If you try compiling without any semicolons after your statement, you'd receive errors.(Try it if you want!)
    --------------------------------------------------
    std::cin.get() <- this basically means "wait for the user to input something". (The reason we use this is to stop the console from closing so fast in a split second, without it, your console would exit out in a blink of an eye)
    --------------------------------------------------
    return 0; <- Remember int main() from before. 'int' means integer. integer means a number. There has to be a value to int, so we return a value to it, in this case, is 0.

    Info about std::
    now later on writing std::function over and over again will be tiring/annoying. To make our lives easier, there is a statement for that.
    Code:
    using namespace std;
    using namespace std; <- this basically means that we'll be using the namespace standard. (not sexually transmitted disease.., standard) By using this, we will no longer have to write std:: for each function that we want to use.
    So by implementing this into our code, we should now have this:
    Code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	cout << "Hello World!" << endl;
    	cin.get();
    
    	return 0;
    }
    Now doesn't that make our lives easier?
    NOTE: Don't worry, I didn't waste your time. the "std::" method may be used at certain times instead of just using this statement.
    --------------------------------------------------
    Now you've created your first c++ program!
    Sorry if this is so messy, I've gotten little sleep today and i've been doing nothing but homework.

  2. The Following 3 Users Say Thank You to merp. For This Useful Post:

    henryarielteam (11-13-2012),Jumboperson (09-09-2012),princemcheese (09-06-2012)

  3. #2
    -Tex^'s Avatar
    Join Date
    Aug 2012
    Gender
    male
    Posts
    14
    Reputation
    10
    Thanks
    0
    good stuff. My C++ professor hated cout so he made us learn printf on our first program lol.

    Thanks for sharing! should be helpful for those new to C++!

  4. #3
    obsta's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Location
    Duncraig
    Posts
    87
    Reputation
    10
    Thanks
    5
    My Mood
    Cold
    good tutorial. However it might be more logical for new users to use system("pause") instead of cin.get because they will be running into that later. just my opinion.

  5. #4
    merp.'s Avatar
    Join Date
    Aug 2012
    Gender
    male
    Posts
    147
    Reputation
    10
    Thanks
    301
    My Mood
    Bored
    Quote Originally Posted by obsta View Post
    good tutorial. However it might be more logical for new users to use system("pause") instead of cin.get because they will be running into that later. just my opinion.
    system("pause") does the same thing as cin.get(), but not using system("pause") is preferred(imo). :v

  6. #5
    Jumboperson's Avatar
    Join Date
    Aug 2012
    Gender
    male
    Posts
    54
    Reputation
    10
    Thanks
    53
    Well thank you! This is very helpful for beginners to programming and is essentially what my java teacher told us(except about java of course). I currently am still an amateur programming only know a few non-compiled languages well (VBS, Java, Batch, Lua). And very few compiled languages, basically Obj. C. This was nice to see the functions of C++.

    EDIT: +1 rep to you!

  7. #6
    merp.'s Avatar
    Join Date
    Aug 2012
    Gender
    male
    Posts
    147
    Reputation
    10
    Thanks
    301
    My Mood
    Bored
    Quote Originally Posted by Jumboperson View Post
    Well thank you! This is very helpful for beginners to programming and is essentially what my java teacher told us(except about java of course). I currently am still an amateur programming only know a few non-compiled languages well (VBS, Java, Batch, Lua). And very few compiled languages, basically Obj. C. This was nice to see the functions of C++.

    EDIT: +1 rep to you!
    Glad to see that this is actually helping a few people.

  8. #7
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    If you create an empty project instead of Win32 console, You dont need to delete anything and its cleaner
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  9. #8
    Jumboperson's Avatar
    Join Date
    Aug 2012
    Gender
    male
    Posts
    54
    Reputation
    10
    Thanks
    53
    Quote Originally Posted by merp. View Post
    Glad to see that this is actually helping a few people.
    Yes it is, I am quite new to C++ and I am still learning some basic commands the most advanced thing I made is a simple php hack for a website game.

  10. #9
    unspeakable's Avatar
    Join Date
    Mar 2012
    Gender
    male
    Location
    WorldWide
    Posts
    73
    Reputation
    21
    Thanks
    7
    My Mood
    Dead
    Quote Originally Posted by obsta View Post
    good tutorial. However it might be more logical for new users to use system("pause") instead of cin.get because they will be running into that later. just my opinion.
    not really, its true that they'll be using cin.get later on but its very good practice to start off with it too, there are hundreds of articles explaining why you shouldnt use system(''pause'') However, it is not encourage to use system("pause") to pause command windows from closing in C++. There are two main reasons why:-

    1. A call to system("pause") will pause the programs execution and make a system call and thus allocate unnecessary resources.
    2. Another reason it should be avoided is because it isn't portable. It works only on systems that have the PAUSE command at the system level, like DOS or Windows.

    That means, when you do use system("pause"), it says: Press any key to continue...

    I reckon system("pause") is introduced to new beginner to make their life easier in learning and exploring programming language. Just like how we were taught that gravity is equal to 10 when we were in primary/secondary school, and then few years later they told us that actually we should calculate gravity as 9.81.You wouldn't tell a little boy that he should calculate gravity as 9.81 while he is still struggling with multiplication involving decimal, would you??
    "out out , brief candle" life is a matter of seconds.

  11. The Following User Says Thank You to unspeakable For This Useful Post:

    obsta (10-02-2012)

  12. #10
    Akar's Avatar
    Join Date
    Jun 2011
    Gender
    male
    Location
    İstanbul
    Posts
    40
    Reputation
    10
    Thanks
    0
    My Mood
    Cool
    #include <iostream>
    #include <conio.h>
    int main(){
    printf("Hello Merp ");
    system("pause");
    return 0;
    }

  13. #11
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    Quote Originally Posted by Akar View Post
    #include <iostream>
    #include <conio.h>
    int main(){
    printf("Hello Merp ");
    system("pause");
    return 0;
    }
    You include <iostream> but yet you use printf (stdio.h)
    You include conio for no reason.

    No thanks.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

Similar Threads

  1. Hello World Disassembly
    By why06 in forum Assembly
    Replies: 15
    Last Post: 01-21-2010, 09:21 AM
  2. New o C++? Simple Hello world (Added a little extra)
    By headsup in forum C++/C Programming
    Replies: 10
    Last Post: 11-09-2009, 06:00 PM
  3. Hello World Anyone?
    By B1ackAnge1 in forum Assembly
    Replies: 11
    Last Post: 11-09-2009, 01:14 PM
  4. A closer look at Hello World App.
    By headsup in forum Java
    Replies: 5
    Last Post: 10-24-2009, 12:25 AM
  5. [C++]Hello World; Your first C++ Program
    By Mr. Bond in forum Programming Tutorials
    Replies: 3
    Last Post: 02-09-2009, 08:53 AM