Be a Supporter!

C++:Getting Started

  • 2,655 Views
  • 23 Replies
New Topic Respond to this Topic
liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
C++:Getting Started 2006-02-18 06:21:45 Reply

C++: Main

So you've heard about this progamming-whatchamacallit and you heard it can be used to make games or software but you have no idea how to get started. Well, the first thing you need to do is download a program that will be able to compile the C++ code. There are many different programs out there and they all have their own pros and cons, so one program that is great for one person may seem crap to you.

The program I use is called Dev-C++ by a little company called Bloodshed Software. The page is here and the direct-link to the download is here. Note that there are many versions, that one is the latest beta of Dev-C++ 5 - you can download Dev-C++ 4 here if you want to be sure the program is stable.

You can get a load of others such as Microsoft Visual C++, among others.

Ok, so now you've downloaded your C++ editor/compiler and are ready to start coding this amazing game of yours - but theres a catch. You actually need to learn how to program first. So what you want to do is start off with the basics, then build your way up until you are good enough to put it all into practice and build a real game.

The first thing you'll learn is how to compile basic programs that run in the command prompt window. So start a new C++ source file (File>New>Source File) then type the code below into the editing area:

#include <iostream>
using namespace std;
int main(){
cout<<"Hello World\n";
system("pause");
return 0;
}

Then compile the program and run it (Execute>Compile & Run), to see what it prints on screen.

The first thing you do with a C++ program is include the necessary librarys, you do this by typing #include then the name of the library you are wanting to import - for example:

#include <math.h>
#include "myheader.h"

Will include the math library and the myheader (a fictional header you could create yourself) header file. Librarys are necessary because they are what contain all of the functions you need to create your program. Without them the functions won't work. You should also always add the line:

using namespace std;

because it allows you to use the standard library (you need it to use functions like cout and cin as long as you include the iostream library as well).

Another thing to remember is that you need to end all lines with a semi-colon (a ;) or else it will bring up an error because the compiler won't know that the line has ended.


Sup, bitches :)

BBS Signature
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Online!
Forum Stats
Member
Level 19
Programmer
Response to C++:Getting Started 2006-02-18 08:11:31 Reply

Very basic introductions which is good I suppose for a complete beginner who doesnt know what a compiler is. I think a follow up tutorial or 2 would be cool.


- Matt, Rustyarcade.com

Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to C++:Getting Started 2006-02-18 08:25:01 Reply

there is no need to call system command, that's not valid C++ since it depends on the OS

getch() would've done the trick and for you people who like doing it correct so would while(!kbhit()){} although I'm not sure the two are included in iostream

Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Online!
Forum Stats
Member
Level 19
Programmer
Response to C++:Getting Started 2006-02-18 08:27:38 Reply

I always put
getchar ();
Oh a note to newbies who get bloodshed's dev C++. The program will end unless you use something like getchar (); before the end of the main() function. Remember that


- Matt, Rustyarcade.com

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to C++:Getting Started 2006-02-18 09:15:01 Reply

At 2/18/06 08:25 AM, Inglor wrote: there is no need to call system command, that's not valid C++ since it depends on the OS

getch() would've done the trick and for you people who like doing it correct so would while(!kbhit()){} although I'm not sure the two are included in iostream

I always use cin.get(); or cin.ignore(); (or both..), or just stick an endless loop (k: goto k;) at the end if I'm just writing something quickly.. I never use system when I'm coding, but I thought I'd use that because it's easier to explain.


Sup, bitches :)

BBS Signature
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Online!
Forum Stats
Member
Level 19
Programmer
Response to C++:Getting Started 2006-02-18 11:31:16 Reply

alternatively you could put a while loop around the contents of main (or at least the contents you want to keep running) and break whenever you want to end the function and have some kind of input any time you want it to stop temporarily


- Matt, Rustyarcade.com

Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to C++:Getting Started 2006-02-18 11:38:54 Reply

cin.get getch and getchar actually give the system input , you can use i=getch() if I is a char and it would get input !kbhit() pretty much makes logical sense too which is why I use it, cin.ignore() is C++ and I always like doing stuff the c way just in case unless the C++ way is better (hence why I still use printf most of the time :P)

DJ
DJ
  • Member since: Aug. 9, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to C++:Getting Started 2006-02-18 12:15:04 Reply

i took a c++ class and it seems that the program iwas using was much easier. i as called borland c++. this version was a very old one. my teacher told me it has been on the schools servers for more than 5 years. he said he hasnt told them to upgrade it is because it is a great program, and i agree with him. but i think that what we learned was probably a simple version of it but we did get to do some neat stuff.

dburford11
dburford11
  • Member since: Apr. 1, 2005
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to C++:Getting Started 2006-02-18 13:21:20 Reply

Yes, Inglor was right, a Hello World looks like this:

#include <iostream>
using namespace std;
int main()
{

cout<<"Hello World";
cin.get();
return 0;

}

you should make another tutorial teaching people how to do variables, and stuff like that.

Greeley
Greeley
  • Member since: Aug. 30, 2005
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to C++:Getting Started 2006-02-18 13:38:52 Reply

Thanks man. When I got time on my hands I'll know where to start!

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to C++:Getting Started 2006-02-19 08:20:51 Reply

At 2/18/06 08:24 PM, back_to_blackvector wrote: Just because everyone here has no life and no job and cant afford a real C++ IDE with a compiler dont get hasty with them.

What are you talking about? All compilers would close the window straight away because the program has ended, so if the window didn't close then that would be a BAD thing.


Sup, bitches :)

BBS Signature
Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to C++:Getting Started 2006-02-20 08:40:52 Reply

Yeah, you've gotta get prepared to C++ newbs.

'k but Liam, how can I code C++?' :P


BBS Signature
qiushidaren
qiushidaren
  • Member since: Jan. 16, 2006
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to C++:Getting Started 2006-02-20 10:18:26 Reply

Very good explanation, it is easy to be understood by greenhorns.

Cryzabey
Cryzabey
  • Member since: Jun. 27, 2006
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to C++:Getting Started 2006-07-27 10:49:06 Reply

Another thing to remember is that you need to end all lines with a semi-colon (a ;) or else it will bring up an error because the compiler won't know that the line has ended.

Umm you lost me, Then how come "int main()" doesn't end with a semi-colon?


BBS Signature
elbekko
elbekko
  • Member since: Jul. 23, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to C++:Getting Started 2006-07-27 10:56:05 Reply

At 7/27/06 10:49 AM, Cryzabey wrote:
Another thing to remember is that you need to end all lines with a semi-colon (a ;) or else it will bring up an error because the compiler won't know that the line has ended.
Umm you lost me, Then how come "int main()" doesn't end with a semi-colon?

Because it is a function declaration


"My software never has bugs. It just develops random features. " - Unknown [ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature
VirusN
VirusN
  • Member since: Sep. 20, 2007
  • Offline.
Forum Stats
Member
Level 23
Programmer
Response to C++:Getting Started 2008-11-09 13:29:55 Reply

great

henke37
henke37
  • Member since: Sep. 10, 2004
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to C++:Getting Started 2008-11-09 14:41:47 Reply

let's see here, system should be banished from the c++ standard since it is totally unportable, does not do anything that can not be done better with a platform api and is wildly abused to do stuff that should have been done with imported libraries instead.
Also, it is considered a bad practice to import the full std namespace, since it can contain a lot of identifiers that you might not expect.


Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to C++:Getting Started 2008-11-09 14:56:34 Reply

At 11/9/08 02:41 PM, henke37 wrote: let's see here, system should be banished from the c++ standard since it is totally unportable, does not do anything that can not be done better with a platform api and is wildly abused to do stuff that should have been done with imported libraries instead.
Also, it is considered a bad practice to import the full std namespace, since it can contain a lot of identifiers that you might not expect.

Fine..

#include <iostream>
using std::cout;

int main(){
cout<<"Hello World!";
return 0;
}
This is a 2 year old post......... and I broke the page with an unclosed bold statement, awesome;)

</bold>


Sup, bitches :)

BBS Signature
FallenSpirits
FallenSpirits
  • Member since: Aug. 30, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to C++:Getting Started 2009-09-07 21:19:28 Reply

im completely lost in this

TheFantasyClub
TheFantasyClub
  • Member since: Nov. 29, 2010
  • Offline.
Forum Stats
Member
Level 14
Musician
Response to C++:Getting Started 2011-10-08 07:27:44 Reply

At 11/9/08 02:56 PM, liam wrote:
At 11/9/08 02:41 PM, henke37 wrote: let's see here, system should be banished from the c++ standard since it is totally unportable, does not do anything that can not be done better with a platform api and is wildly abused to do stuff that should have been done with imported libraries instead.
Also, it is considered a bad practice to import the full std namespace, since it can contain a lot of identifiers that you might not expect.
Fine..

#include <iostream>
using std::cout;

int main(){
cout<<"Hello World!";
return 0;
}

This is a 2 year old post......... and I broke the page with an unclosed bold statement, awesome;)
</bold>

One of the big mistakes in coding (I made a post yesterday) is the coding style.

Why not put void inside the arguments of main function? It isn't a declaration?
Why put braces in the same line of the declaration? Why not after?

#include <iostream>
using std::cout;

int main(){
cout<<"Hello World!";
return 0;
}

I would change to (newbies comments added, too)

/*
 This is a C style comment. You can erase it or not, whatever you want.
 The C style comments are multi-line. It's good sometimes.
 Usually the programmer uses comments to made their code easier to develop.
*/
// This is a C++ style comment. This kind of comments use only one line. Very useful

#include <iostream>
// The #include is a directive, who inserts functionality to write in a console output, in this example.

/*
 Main function. The definition of the function goes here.
 Receives: void (it doesn't need anything)
 Returns: int
 When you want to make a program, use this kind of comments for explain what do you want.
 Example: I want to send to std::cout the message [Hello World!] and, then, finish the app.
*/
int main(void)
{
  std::cout << "Hello World!"; // This is a commentary beside code. Use them!
  return 0; // The function main returns a int, zero.
}

Why [std::cout] instead [cout]? Simply: [using namespace] it's unexplained in basic levels, and must be avoided to use until explained.
Why so much comments? It's a program made for beginners, it's neccessary!

Remember you are introducing to coding: the people who doesn't ever made a program.


The Fantasy Club - Audio Composer pseudo-artist. C/C++ Programmer most of time. Advanced in C and newbie/mid level in C++ (templates... i hate you!)

neolinked
neolinked
  • Member since: Dec. 4, 2010
  • Offline.
Forum Stats
Member
Level 03
Musician
Response to C++:Getting Started 2011-10-08 14:38:25 Reply

Thanks for making the incomprehensible a little bit more understandable! haha


Click pic for my best trance song

BBS Signature
Wolfos
Wolfos
  • Member since: Jan. 19, 2008
  • Offline.
Forum Stats
Member
Level 25
Game Developer
Response to C++:Getting Started 2011-10-09 09:57:33 Reply

Wow... If I read that correctly you're seriously recommending DevC++? That hasn't been updated in 10 fucking years and doesn't work anymore in Windows 8!

Use Code::Blocks.

Wolfos
Wolfos
  • Member since: Jan. 19, 2008
  • Offline.
Forum Stats
Member
Level 25
Game Developer
Response to C++:Getting Started 2011-10-09 09:58:42 Reply

Oh, and #include doesn't actually include a library. It includes a header.

TheFantasyClub
TheFantasyClub
  • Member since: Nov. 29, 2010
  • Offline.
Forum Stats
Member
Level 14
Musician
Response to C++:Getting Started 2011-10-11 05:41:27 Reply

At 10/9/11 09:58 AM, Wolfos wrote: Oh, and #include doesn't actually include a library. It includes a header.

That's almost complete. Usually, the header is a part of a library itself, indeed, think of it like "insert the bookmarks" of functions made previously by other people (or you, maybe).

The compiler (the one it isn't the code and makes the executables based in the code) must know the libraries; the code doesn't need when the programmer hammers code.

This thread was made in 2006, code::blocks wasn't even distributed like in 2011. Look:
Posted at: 2/18/06 06:21 AM <-- The real date.

When I try to make something in Linux, I use Geany. Looks good and seems very similar to Dev-Cpp. The scripts support made other languages available, like fortran.

Sorry if I had lots of fails in my orc-o-graphy.


The Fantasy Club - Audio Composer pseudo-artist. C/C++ Programmer most of time. Advanced in C and newbie/mid level in C++ (templates... i hate you!)