GameTalk Help | Feedback
Complaints | Requests




Welcome to GameTalk! Enter a word or two from the title of your favorite game:

Then click Search to find the forums where you can get help and exchange tips for those games.
Sample Searches (click to try them): pokemon | mario | zelda | baseball | star wars | wii sports
Hint: The newest games are listed first, so scroll down to find forums for older games.


GameTalk   Requests Complaints Feedback Mod Info GT Help
by Request   RPG Chat Metroid Chat | Metroid Games Jedi Sith | Halo Chat Middle Earth The Pointless Forum | duh
by Request   badlands | Axem Mario | Yoshi | Game Dev Sports | Superbowl Homework | College FanFics | Writers
Game Genres   Fantasy | Sci Fi Wrestling | E-Fed Dueling Cards Classic Games Virtual Pets
Popular Worlds   Pokémon | Pokémon Games Star Wars | Star Wars Games Zelda | Zelda Games Harry Potter | Sonic Digimon | Dragonball
New Platforms   Xbox 360 PlayStation 3 Nintendo Wii Nintendo DS | WFC | PSP Game Slackers
New Platforms   Wii vs PS3 vs 360 GameCube vs PS2 vs Xbox Nintendo DS vs Sony PSP PC Games | Mac Games Phone & Smart Devices
Other Media   Music Movies TV | [adult swim] Books | Graphic Novels Anime
Special Topics   World Life | Spirituality Peace | Holiday Future | Past Depression | Health


 Questions  Your Reviews  Tips  Polls  Chat
View Questions
Ask One
See Reviews
Write One
Best | All Tips
Give One
See Polls
Start One
View Chat
Post One


Game Developers Forum

A forum where game creators and game-related website creators can come together to share ideas and exchange tips.
Moderated by sub_reb
forum guidelines  |  forum news







Tip for
Provided by: mew 1128
Feb 20, 2008
C++ Tutuorial 1: Introduction & Basic program structure
This tip is currently ranked 3.6 out of 5 by a bunch of voters.
mew 1128 has earned 3 gems!

Well due to the lack of any programming tutorials here I figured I would post one.

I will be writing new tutorials as I find time which should allow me to release every few weeks or so.

First, C++ is a popular and powerful programming language used in many large scale games and applications. Although C++ is a rather difficult language compared it's predecessors (such as C#) it makes up for that with it's speed and how much you can do with it.

I will be using the Microsoft Visual C++ 2008 express edition compiler and basing these tutorials around it. This is, at least in my opinion, a very good compiler and it is absolutely free. You can only compile AND run your projects on a windows based operating system that have the .NET framework installed. So that means if you want to write C++ programs for any other operating system you must use a different C++ compiler. Most other C++ compilers should compile the native code with few changes but are unable to compile managed C++ code.

One last thing to consider is that I am only 14 years old and NOT a professional. This means that, although they work, I may use some techniques that are not considered good practice or that have an easier way to preform.

Alright with that over lets move on to writing our first program.

Open up Visual C++ and under Win32 projects select the Win32 console application. Give your project a name and push OK. When the wizard comes up, push next and check the option "Empty Project". Push finish and you should see, in the solution explorer, your project and three blank folders below it. Right click the source folder and choose the option add - new item. Select, under the code section, the .cpp file, name it main.cpp, and push add.

.cpp files contain the working code for your program. There are also .h files that contain class, struct, function definitions, and preprocessor dierctives. I'll get into .h files more next lesson but for now don't worry about them.

Before we can do anything in a program we must define an entry point. All executable programs and libraries have entry points. They are simply where the program begins.

Lets define an entry point to our application, type the following code in main.cpp:

int main() { return 0; } Let's go through this line by line.

First, int main() defines the main function of the program. Regardless of where you put it, the program will always start by running main. The "int" before it specifies that it returns a value that is of the type int. An int stores a number using 4 bytes of memory and can range from -2.1 to 2.1 billion. Notice that after main there is a pair of ()'s. These are required to define a function and without them you would be defining a variable, or a word associated with an area of memory that you can store data in.

Next we have a "{". This means that everything after the { and before the } will be in the scope of the function main.

Then we have the statement return 0;. When a function reaches a return statement it terminates and "returns" the value after it, if any, to what called it, in this case the operating system. The semi-colon ";" after the statement simply means that it is the end of the statement. Returns are not necessary when programming C++ since the ; ends the statement but they make the code a lot easier to read and follow.

Finally, we have the } which signals the end of the function.

Now that we have a working program let's make it do something. Add the line "#include " (without the quotes) at the top of the file and the line 'std::cout

int main() { std::cout

Build Solution. This will compile the project in to a .exe file. After that in the same menu click on Debug - Start without debugging. You should see a black console window displaying the words "Hello!".

Ok so lets go over how this works. First the line #include includes a library of functions (such as cout and endl) that deal with input and output to the console and external files. The # specifies that the command following is a preprocessor directive that the compilers preprocessor is to handle.

Next we added the line 'std::cout

So, is this tip helpful? Vote here! Great tips will be ranked higher, and bad tips will be tossed. Please just vote once for each tip (and don't vote for your own tip- we'll know!)...

  Pick One:
  5 - Simply brilliant and very helpful!!
  4 - It's very good and it works!
  3 - It's a pretty good tip.
  2 - It's a tip, but it's kind of dumb.
  1 - I'm just not sure about this one...
  0 - It doesn't work!
  0 - It's not even a tip!!

There are 6 Replies:
Message Person and Time

Next we added the line 'std::cout

mew 1128
Feb 20, 2008
(78 days and 16 hours ago)

Continued

'std::cout

mew 1128
Feb 20, 2008
(78 days and 16 hours ago)

Seems the formating of text doesn't allow greater than or less than signs to be added so I'll have to re-write the end of it using the words.

Now that we have a working program let's make it do something. Add the line "#include (less than sign)iostream(greater than sign)" (without the quotes) at the top of the file and the line 'std::cout (2 less than sign without a space) "Hello!" (2 less than sign without a space) std::endl;' (again without the quotes) above the line "return 0;". You code should looks something like:

#include (less than sign)iostream(greater than sign)

int main() { std::cout (2 less than sign without a space) "Hello!" (2 less than sign without a space) std::endl; return 0; } (Note that the spaces and returns before each statement are to make it easier to read and don't change the program at all).

On the menu at the top of the window click on Build - Build Solution. This will compile the project in to a .exe file. After that in the same menu click on Debug - Start without debugging. You should see a black console window displaying the words "Hello!".

Ok so lets go over how this works. First the line #include (less than sign)iostream(greater than sign) includes a library of functions (such as cout and endl) that deal with input and output to the console and external files. The # specifies that the command following is a preprocessor directive that the compilers preprocessor is to handle.

Next we added the line 'std::cout (2 less than sign without a space) "Hello!" (2 less than sign without a space) std::endl;'. Lets break this into sections. First the std:: means that what directly follows is in the namespace std. A namespace is a way to keep functions separate from other functions that may have the same name. In this way, a function that isn't under a namespace called cout could be defined differently than the same function under the namespace std. Notice the '::' after std. This is a basic C++ the operator meaning that what is on the right belongs to what is on the left.

Next we have cout. This is a stream that is linked to the command line. Anything put into this stream by default will be displayed on the command line. Next is '(2 less than sign without a space)'. When used with streams this means output what is on the right into the stream on the left. In this case on the right we have the text "Hello!" and on the left the stream cout.

After that we have another instance of the '(2 less than sign without a space)' operator and std::endl. This flushes the buffer and outputs a line return. 'endl' also resides in the namespace 'std'.

Although this program is far from impressive it is structured the same as large applications. Any program you write will consist of preprocessor directives, an entry point, and commands residing in namespaces.

Sorry for the multiple posts but it is impossible to use this without the correct operators. Make sure you replace the text with the actual less than and greater than signs.

mew 1128
Feb 20, 2008
(78 days and 16 hours ago)

Ohh it's not such a good idea to be posting tutorials like this unless you know how to format it properly for GT. Use & gt; and & lt; for > and < (no spaces to get those and the ";" isn't necessary).

Another problem with your formatting is GT's line spacing, as you've noticed. There isn't really any way around that unless you double space every single line. Tabbing over for spaces will also not work, in that case use a bunch of & #nbsp; for spaces (again, no space after the "&" for that to work.

LLight
Apr 27, 2008
(11 days and 16 hours ago)

Err, yeah sample:

-------

#include <iostream>

using namespace std;

int main()

{

&#nbsp; &#nbsp; &#nbsp; cout

LLight
Apr 27, 2008
(11 days and 16 hours ago)

Uh oh, I forgot to remove those < < for the console out lines. That's why it got cut off. Eh, silly me, force of habit. And it was & nbsp;, not &#nbsp;

-------

#include <iostream>

using namespace std;

int main()

{

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

      return 0;

}

-------

I use Bloodshed Dev-C++ because that's what my college uses. It needs system("PAUSE"); before the retun 0; for that to work.

You probably also should have mentioned how to load libraries for any of this code to work. That can be accomplished by putting the lines "#include <iostream>" and "using namespace std;" above your program.

LLight
Apr 27, 2008
(11 days and 16 hours ago)


  • Add Your Own Reply
  • Back to Index


  • Xbox 360   Halo 3 Call of Duty 2 | Three | Four MW The Elder Scrolls IV: Oblivion Gears of War | Two Rainbow Six Vegas | GRAW
    Xbox 360   BioShock Grand Theft Auto IV Viva Pinata Mass Effect Xbox 360 | Xbox Live Arcade
    XBox   Halo | Halo 2 Fable ES 3: Morrowind | GOTY Doom 3 Star Wars: KOTOR | Two
    XBox   Ninja Gaiden Splinter Cell | Chaos Theory Star Wars: Battlefront | Two GTA 3 & Vice City | San Andreas Xbox Live | XBox Chat
    PC Games   Halo | Age of Empires Spore | BioShock Harry Potter: One | Two | Three Final Fantasy VII | VIII | XI F.E.A.R.
    PC Games   RuneScape | Maple Story Sims Two | University | Nightlife GTA 3 | Vice City | Andreas Diablo II | LOD Rise & Fall: Civilizations at War
    PC Games   Half Life | Two | Counterstrike Morrowind | Tribe | BM | Oblivion Starcraft | Two | Warcraft 3 Guild Wars | Factions PC Chat
    WoW   WoW Main | Profiles | Requests Newbies | Pros | Masters Quests | Lands | Items PvP | Guilds | Classes & Races Dragon Lounge | Moonlit Garden



    PlayStation 3   Metal Gear Solid 4: GotP Call of Duty 3 | Four Grand Theft Auto IV Heavenly Sword Warhawk
    PlayStation 3   Sonic the Hedgehog Uncharted: Drake's Fortune Medal of Honor: Airborne Gran Turismo HD | MotorStorm PlayStation 3 Chat
    PlayStation 2   Kingdom Hearts | Two Final Fantasy: X | X-2 | XI | XII MGS 2 | Substance | Three GTA 3 | Vice City | Andreas WWE S vs R | 2006 | 2007 | 2008
    PlayStation 2   God of War | Two Guitar Hero | Two | 80s | Three Gran Turismo 3 | Four Dragon Quest VIII PlayStation 2 Chat
    PSP   Kingdom Hearts: BBS Dynasty Warriors | Untold Legends Castlevania: The DXC Crisis Core: FF VII Twisted Metal
    PSP   Metal Gear Acid Patapon GTA: Liberty City Stories God of War: CoO PSP Chat
    PSX   Final Fantasy IX | VIII | VII FF Tactics | Origins | Chronicles Digimon World 2 | Three | RPG Legend of Dragoon | Chrono Cross PSX Chat



    Wii   Super Smash Bros. Brawl Fire Emblem: Radiant Dawn The Legend of Zelda: TP Wii Sports | Wii Play | Wii Fit Marvel: UA
    Wii   Mario Kart Wii Pokemon Battle Revolution Guitar Hero III | Aerosmith Metroid Prime 3: Corruption No More Heroes
    Wii   Mario Galaxy | Strikers Charged Soulcalibur Legends Emergency Mayhem Resident Evil 4: WE | Umbrella Chronicles WarioWare
    Wii   Super Paper Mario Rayman Raving Rabbids | Two Dragon Quest Swords Zack & Wiki Wii Chat
    GameCube   Super Smash Bros. Melee Animal Crossing | Two Mario Sunshine | Kart DD Tales of Symphonia Phantasy Star EP I & II
    GameCube   Resident Evil | Zero | Four Super Paper Mario | TT-YD Sonic DX | Two | Heroes Pokemon Colosseum | XD: GoD Fire Emblem: Path of Radiance
    GameCube   HM: AWL | Another WL | MM Pikmin | Two Zelda WW | OoT | Bonus Disc Zelda: Twilight Princess GameCube Chat
    Nintendo DS   Super Mario 64 DS | Mario Kart DS The World Ends With You Final Fantasy III | CC: RoF Animal Crossing: Wild World Legend of Zelda: PH
    Nintendo DS   Pokemon Mystery Dungeon: EoD | EoT Pokemon Diamond & Pearl | Wi-Fi Pokemon Ranger | MD: BRT | Battonage Guitar Hero: On Tour Nintendo DS Chat | WFC
    GameBoy Adv   Pokemon R&S | Ruby | Sapphire Fire Red & Leaf Green | Emerald Advance Wars | Two Fire Emblem | Sacred Stones Zelda: ALttP | Minish Cap
    GameBoy Adv   YuGiOh! EDS | WWE | SC Kingdom Hearts: CoM Golden Sun | Two FF Tactics Advance GBA Chat
    Nintendo N64   Paper Mario | Super Mario | SSB Zelda OoT | Majora's Mask Banjo-Kazooie | Banjo-Tooie GoldenEye 007 | Perfect Dark N64 Chat
    GameBoy   Pokemon: Crystal | G&S | R&B DWM | Tara | Cobi Zelda: OoS | OoA | LA Harry Potter YuGiOh! DDS



    Welcome to GameTalk: Getting Started | More About Us | Mod FAQ & Guidelines | Terms of Use

    Copyright © 1995-2008 GameDex Inc.