We are currently looking for people to help us make game-related help pages to assist players with common problems.
posted by Tap on Apr 14, 2010

C++ is the hardest language I ever had to learn. It's useful, but it's frustrating. If you're new to programming, I suggest you try something simpler to wet your palette before jumping into the complex syntax of C++.

Before we begin, I expect you to understand the basics of programming. For example, you should know what arrays are. Also, you should have an editor/compiler IDE ready. If you need guidance, I recommend CodeBlocks as the editor and MinGW (simpler version of GCC for Windows) for the compiler. Both are completely free software. CodeBlocks optionally has MinGW built into it.

The very first thing you need to do in writing your first program is include the required libraries. The required libraries depend on what you want to do, but you almost always need to include iostream and string, like so:

#include <iostream>
#include <string>

After including the libraries you're going to use, you should make life easier by assigning the standard library to the main program, like so:

using namespace std;

This reduces the redundancy of typing std:: before every string or input/output declaration. This means cleaner code. For example, the above line makes you able to type "string stringname" instead of "std::string stringname" and makes your code easier to type. In some cases, removing the std:: prefix can cause conflicts with other code, but this is rare, so I simply advise to remove it whenever possible. Here's the code so far:

#include <iostream>
#include <string>
using namespace std;

Next, you should define any variables the program will use. You can use #define to create permanent references to values or complete lines of code, but let's focus on the creation of a variable:

int points;

The above line of code creates an int or integer named points, but the value of points is not mentioned. This is because we define the values of variables within the main class. Why? Because we may want to restart the program without exiting and reloading. This is a good habit if you make games or anything that loops.

Integers are for numbers. If we want to create a variable that holds text, we use a string:

string playerName = "Josh";

The above line of code creates a string variable named playerName and gives it a value of Josh. I only included the value to illustrate how it's done. Again, it's best practice to set the value within the main class.

What's the main class, you might ask. It's not hard to understand, but the main class is a function that every C++ program has. The main class is run automatically and will loop infinitely until we terminate the program. Here's all our code with the main class and some values being set:

#include <iostream>
#include <string>
using namespace std;
int points;
string playerName;

int main () {
points = 0;
playerName = "Josh";
return 0;
}

The last line of code (aside from the end brace), return 0, is how we terminate or close the program. The above code, if you try compiling and running it, won't appear to do anything and will instantly close, because we don't have any output or anything else happening visually. However, if you run it without CodeBlocks, you'll discover it starts and stops successfully.

To add some interactivity, you'll need to step deeper into the language, and I'm not prepared to guide you any further. I'm still learning, too, but when I feel comfortable with my own knowledge, I'll add onto this guide. In the meantime, here's some code I made for you to toy with:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define PI 3.14159

int playerHP,playerMaxHP,playerMaxMoney,playerMoney,playerInjury,playerChoice1;
char playerName[50];
string exitCode;
string yesorno;
bool answerYes = false;

vector<string> yesornoArray;

void showStats () {
    if (playerInjury >= 1) {
        cout << "\nPlayer: " << playerName << " (INJURED)\n\tPlayerHP: " << playerHP << "/" << playerMaxHP << "\n\tMoney: " << playerMoney << "/" << playerMaxMoney << "\nwww.getlives.com\n";
    } else {
        cout << "\nPlayer: " << playerName << "\n\tPlayerHP: " << playerHP << "/" << playerMaxHP << "\n\tMoney: " << playerMoney << "/" << playerMaxMoney << "\nwww.getlives.com\n";
    }
    cout << "\n";
}

bool checkIfStringMatchArray (string var2Match, vector<string> matchArray) {
    int matchArrayLength = matchArray.size();
    int i=0;
    while (i<matchArrayLength) {
        if (var2Match == matchArray[i]) {
            i = matchArrayLength;
            return true;
        } else {
            i++;
        }
    }
    return false;
}

int checkGameOver () {
    if (playerHP <= 0) {
        cout << "\n\nGAME OVER!\nPress enter to exit the game. ";
        getline (cin,exitCode);
        return 0;
    } else {
        showStats();
    }
}
bool checkYes (string inputvar) {
    if (inputvar == "y" || inputvar == "yes") {
        return true;
    } else {
        return false;
    }
}
int main() {
    yesornoArray.push_back("y");
    yesornoArray.push_back("yes");
    yesornoArray.push_back("n");
    yesornoArray.push_back("no");
    cout << "\n|--------------------|";
    cout << "\n|                    |";
    cout << "\n|   Dark Corridors   |";
    cout << "\n|   by Josh Maines   |";
    cout << "\n|       v0.1.0       |";
    cout << "\n|--------------------|";
    playerMaxHP = 10;
    playerHP = playerMaxHP;
    playerMaxMoney = 10;
    playerMoney = playerMaxMoney;
    playerInjury = 0;
    playerChoice1 = 0;
    cout << "\n";
    cout << "Insert your name and press enter: ";
    cin.getline (playerName,50,'\n');
    showStats();
    cout << "You wake up in a dark room,";
    getline (cin,exitCode);
    cout << "slightly lit by candles hanging on the walls.";
    getline (cin,exitCode);
    cout << "You look to your right and see a door.\n";
    yesorno = "";
    while (yesorno != "y" && yesorno != "yes" && yesorno != "n" && yesorno != "no") {
        cout << "Open the door? y/n: ";
        getline (cin,yesorno);
    }
    if (checkYes(yesorno)) {
        cout << "\n\nYou approach the door...";
        getline (cin,exitCode);
        cout << "\nJust as you're about to turn the knob,";
        getline (cin,exitCode);
        cout << "the door swings open and smacks you in the face. (-2HP)";
        playerHP -= 2;
        getline (cin,exitCode);
        checkGameOver();
        cout << "\nYou see a dark-skinned lady,";
        getline (cin,exitCode);
        cout << "dressed in black leather boots and chainmail,";
        getline (cin,exitCode);
        cout << "standing in the doorway.";
        getline (cin,exitCode);
        cout << "She left the door wide open...\n";
        getline (cin,exitCode);
    } else {
        cout << "\n\nThe door opens,";
        getline (cin,exitCode);
        cout << "and you see a dark-skinned lady,";
        getline (cin,exitCode);
        cout << "dressed in black leather boots and chainmail,";
        getline (cin,exitCode);
        cout << "walk in through the doorway.";
        getline (cin,exitCode);
        cout << "She leaves the door wide open...\n";
        getline (cin,exitCode);
    }
    yesorno = "";
    while (!checkIfStringMatchArray(yesorno,yesornoArray)) {
        cout << "Run past her? y/n: ";
        getline (cin,yesorno);
    }
    if (checkYes(yesorno)) {
        cout << "\nYou dodge past the woman...";
        getline (cin,exitCode);
        cout << "\nAs you run away, the lady yells \"Watch out!\"";
        getline (cin,exitCode);
        cout << "Just as she warns you, spikes gush out of small holes in the floor.";
        getline (cin,exitCode);
        cout << "(-2HP) You now have a major injury.";
        playerHP -= 2;
        playerInjury = 1;
        playerChoice1 = 1;
        getline (cin,exitCode);
        checkGameOver();
        cout << "\nThe lady whips a rope around your waist and tugs you back to her.";
        getline (cin,exitCode);
        cout << "She explains, \"You're lucky I was able to get you before the ceiling-\"";
        getline (cin,exitCode);
        cout << "The ceiling crumbles and blocks the passage way you tried to run through.";
        getline (cin,exitCode);
        cout << "\"-fell.\"";
        getline (cin,exitCode);
    }
    cout << "\n\nThe woman offers to show you out of the dungeon,\n\"I can get you out of here, but it's not going to be easy.\"";
    getline (cin,exitCode);
    cout << "\n\nYou take her up on her offer and both of you continue out of the room.";
    getline (cin,exitCode);
    if (playerChoice1 == 0) {
        cout << "\nShe stops you from stepping onto a spiked floor\nand suggests you follow her down a different path.";
        getline (cin,exitCode);
    } else {
        cout << "\nSince the path you tried before is now blocked off,\nthe lady shows you a different route.";
        getline (cin,exitCode);
    }
    cout << "\n\nAfter evading several traps and navigating narrow paths,\nyou are both at the entrance to a cavern.\nThis is your exit.";
    getline (cin,exitCode);
    cout << "\n\nTHE END.\nPress enter to exit... ";
    getline (cin,exitCode);
    return 0;
}

The code above is a work-in-progress choose-your-own-adventure game. I figured the best way to learn a programming language is to make something interesting.

Ads went here.