Welcome to TRiBot Forums

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

Sign in to follow this  
Followers 0
Kappa

C++ Hello World

7 posts in this topic

I noticed that this section of the forum is completely dead so I thought that I would post a Hello World tutorial, as that is always where newbies start. The images included may be large but I thought it would be better to have them absolutely clear than hard to make out. I am using Visual Studio 2012.

 

Download and install Visual Studio 2012 or Visual C++ Express Edition from the Microsoft website.

 

Now, create a new C++ project. A window will pop up, press next on it and it should bring you to this page:

 

mZzFgi0.jpg?1

 

Select the "Empty Project" box and press Finish. This will create the project without any source or headers in it at all.

 

Now, Head to the Solution Explorer. Right click on Source Files > Add > New Item > C++ file.

 

I974uik.jpg?1

 

Call this new file "Main.cpp". Now to begin writing code; there is not a lot of it.

 

The first thing we need to do is include something called a library which will allow us to get input and output from the console. We do this by typing:

#include <iostream>

where iostream is the name of the library.

 

Under that, write:

using namespace std;

I will explain what this does in a minute. The ; at the end means that it is the end of a statement and this semi colon is found at the end of most lines in C++, take note of this.

 

Now to write the main method. This is what your program will execute when it first starts up. 

int main(){    return 0;}

int means that this method will return an integer value, also known as a whole number. By 'return' I mean that this is what the method "gives back" once it is finished running. I will explain this in a later tutorial. What you need to know for now is that when the main function reaches the end, it returns 0 which tells us that it ran successfully.

 

Inside the main method, we can write to the console window. To do this, insert the following lines inside the braces ( the { } marks ).

cout << "Hello world!" << endl;

Cout means console output. We pass in something called a string value, which is really just a piece of text. A string is always encased by quotation marks. Endl simply means that it is the end of the line, so the next time we call cout, it will be written on a new line. Remember the std namespace that we defined before? If we didn't use that we would have to write std::cout, so we include the std namespace simply to make it more convenient.

 

After this line, write:

cin.get();

Cin stands for console input. This line reads input from the console but doesn't do anything with it. It will stay on this line waiting for you to press the enter key before it will proceed. If we didn't have this line, the window would flash up and vanish after executing.

 

Here is an finished, commented, code incase you have any difficulties:

#include <iostream> //Includes the library needed for console input and output.using namespace std; //Used so that we don't have to type std::cout, just cout.int main() //This is the main method. When the program starts, this will be ran. {	cout << "Hello World!" << endl; //Prints "Hello world!" to the console window. The endl simply ends the line.	cin.get(); //Gets input from the console, in this case it is used to simply wait for the user to press a key.	return 0; //Tells us that the program ran successfully.}

Now click the debug icon at the top, it looks like a play button. This will compile your code and run it.

Edited by Kappa

Share this post


Link to post
Share on other sites

Omg, why are you declaring the entire namespace for std, you're just allocating so much space for such little requirement, causing memory leaks. Just use it via:

std::cout << " Hello World!" << std::endl;

The method you did is the lazy method, which will in the long run, cause more leaks and lag.

 

Return 0 doesn't tell us if it ran successfully, it voids all of the code preceding it, or if you may, "trash" the code once the task is completed, you can have errors in the preceding code and it will still return 0 in the end, since that's the integer you have inserted, so no, return 0 does not mean it has been successful, it's only telling the program what to do once everything else has been completed.

Edited by arun823

Share this post


Link to post
Share on other sites

Well that right about not meaning the program/function was successful. But it will not necessarily void the entire code before it. You can use static/global variables and return 0, and the global var will not be trashed at all. Return 0 simply returns 0 to a function. So if the function "main" were called at a later time, it would return 0 where it is called. You must return some int value if you are using "int main()" This is the basic way to make a main function. It is perfectly fine. 

Share this post


Link to post
Share on other sites

Omg, why are you declaring the entire namespace for std, you're just allocating so much space for such little requirement, causing memory leaks. Just use it via:

std::cout << " Hello World!" << std::endl;

The method you did is the lazy method, which will in the long run, cause more leaks and lag.

 

Return 0 doesn't tell us if it ran successfully, it voids all of the code preceding it, or if you may, "trash" the code once the task is completed, you can have errors in the preceding code and it will still return 0 in the end, since that's the integer you have inserted, so no, return 0 does not mean it has been successful, it's only telling the program what to do once everything else has been completed.

 

Hard to explain how fucking retarded you are kid. Time to burn yourself with matches.

Share this post


Link to post
Share on other sites

Omg, why are you declaring the entire namespace for std, you're just allocating so much space for such little requirement, causing memory leaks. Just use it via:

std::cout << " Hello World!" << std::endl;

The method you did is the lazy method, which will in the long run, cause more leaks and lag.

 

Return 0 doesn't tell us if it ran successfully, it voids all of the code preceding it, or if you may, "trash" the code once the task is completed, you can have errors in the preceding code and it will still return 0 in the end, since that's the integer you have inserted, so no, return 0 does not mean it has been successful, it's only telling the program what to do once everything else has been completed.

 

Actually,

the return value of 0, is commonly used as the verifier value of a successful call to a function.

Where -1 and 1 is often returned to verify that something have gone wrong.

However, when it comes to the main function itself,

The C++ Standard requires main to return an int value by the runtime library as an exit code for the process.

However, the C standard allows you to do int main(void).

Share this post


Link to post
Share on other sites

Omg, why are you declaring the entire namespace for std, you're just allocating so much space for such little requirement, causing memory leaks. Just use it via:

std::cout << " Hello World!" << std::endl;

The method you did is the lazy method, which will in the long run, cause more leaks and lag.

 

Return 0 doesn't tell us if it ran successfully, it voids all of the code preceding it, or if you may, "trash" the code once the task is completed, you can have errors in the preceding code and it will still return 0 in the end, since that's the integer you have inserted, so no, return 0 does not mean it has been successful, it's only telling the program what to do once everything else has been completed.

 

That's not a namespace declaration, that would be something like

namespace identifier{  entities}

Also it's not 'allocating so much space' and isn't going to cause memory leaks or lag...

 

Not sure how his method is lazy either, it's perfectly valid, if a little verbose (which, really, is the opposite of lazy). Returning 0 at the end of the method is a way of identifying if the method has been executed correctly, as if there's been an issue in a preceeding piece of code, another number (e.g. 1) is returned.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.