C++: A Gaming Approach – 1

This is going to be the first of (hopefully) many tutorials where I teach you how to program in C++ using a gaming approach. I’ll be giving you sections of code, then afterwards I’ll explain what each line does. I’ll also highlight the lines of code I will be explaining.

#include <iostream>

int main()
{
    std::cout << "Hello, Gamer!" << std::endl;

    return 0;
}

1: This line tells the preprocessor to include the iostream standard file. You will need to include this file every time you want to use input or output. Because line 5 has an output we will need this file in our program.

3: This line starts your definition of your main function. All programs need a main function as this is the function that always gets ran first. int means that the function will return an integer(line 7). Since there is nothing between the open and closed parenthesis, this function doesn’t take in any parameters. I will talk about parameters in a later tutorial.

5: This line tells the program to output “Hello, Gamer!” to the console. cout tells the program that it will be outputting something. endl tells the program to end the current line and go down to the next one. You need to include std:: before cout and endl because they are part of the standard C++ library. I’ll be showing you an easy shortcut to get rid of this later on in the tutorial.

7: The return statement in this line causes the main function to finish. Returning a value of zero, usually means that the program ran without any errors.

Now, when you compile and run the program it should compile without any errors, but the console will pop up and quickly go away. To solve this we only need to add one line of code.

#include <iostream>

int main()
{
    std::cout << "Hello, Gamer!" << std::endl;

    std::cin.get();
    return 0;
}

7: This line will pause the program and wait for some kind of user input. Notice cin is for getting input, while cout is for printing output. .get() tells the program to wait for the user to press any key before moving on.

Now when you run your program it should print “Hello, Gamer!” and then pause. When you press a key the program will return 0 and close. Some programmers strongly discourage the use of cin.get() so here is another way to accomplish the same thing:

#include <iostream>

int main()
{
    std::cout << "Hello, Gamer!" << std::endl;

    system("pause");
    return 0;
}

7: Now instead of waiting for input from the user, we are telling the system to pause until the user presses a key. Not only will this command wait for the user’s input, but it will also prompt them with the message “Press any key to continue . . .” This will usually be the method you want to use so the user knows that they need to provide some kind of input before the program will continue running.

Alright, now onto the shortcut I mentioned to get rid of the std::. Once again, this will be as easy as adding one line of code.

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello, Gamer!" << endl;

    system("pause");
    return 0;
}

2: This line will tell the preprocessor that the program is using the std namespace. You will want to have this line of code in every program that you use input or output so you don’t have to type std:: before all of your cin, cout, and endl‘s.

Everyone likes having options, so the last thing I want to show you in this tutorial is an alternate to using endl to end the line and then some other shortcuts that we will be using later on.

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello, Gamer!\n";

    system("pause");
    return 0;
}

6: The only difference between this code and the previous is that I took out endland added \n to the end of my output. \n is just another way to tell the program to go to the next line. To skip multiple lines you can easily change this to “Hello, Gamers!\n\n\n” and that will go to the next line three times.

Here are some similar shortcuts that you can use:
\t: tab
\r: carriage return
\v: vertical tab
\b: backspace
\f: form feed
\a: alert
\': single quote
\": double quote
\?: question mark
\\: backslash

I hope that you learned a lot from this tutorial. If you have a comment or need to ask a question about anything in this tutorial please comment on the article. Please do not email me any questions or comments. This is for two reasons. First, it will help keep my inbox clean. Also, it will be out there for everyone to see which will reduce the same questions being asked multiple times and when I reply, everyone will see it, unlike if I send an email to you. Thanks and don’t forget to check back for future tutorials!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s