PDA

View Full Version : [C++] Your first program


ToshiXZ
11-15-2008, 01:16 AM
Hey guys. I'm 9000234 and I don't mind helping you all out starting programming.
What I am going to assume:
You know WHAT programming is.
You have administrative privileges on the computer you are programming on
You are using Windows. (You can use Mac or Linux, but I cannot fully cover the installations of compilers on those OS's)
You understand the language English to at least a grade 5 level


Part 1
Introduction:
Programming is thought by many to be nerds standing over computers saying 110101110101. This is quite untrue. Yes, there is a programming language and binary (the 1's and 0's is learnable and programmable, but why would we do that when we can type "Hi!"; ? The first thing we are going to use is an IDE. An IDE is an Integrated Development Environment. You may have heard of some of them before, Dev-C++, Microsoft Visual Studio, Code::blocks, etc. Doesn't matter if you have. Basically it's a text editor like notepad, but looks a bit like Microsoft Word. You type in your code, and use the compile or make button to turn the program into an .exe. You may be thinking why would we need an IDE if code isn't 1's and 0's. When we write code in an IDE and compile it, the compiler in the IDE turns our human-readable code into computer friendly code. We don't need to know much about this for now, just it does its job, so we don't need to worry about it.

Part 2
Getting an IDE:
As I said before, there are many IDEs available. Almost all are free, and all the best ones are free. I'm going to give you some download links for some.
I personally use VC++ 2009, Microsoft Visual Studio, Borland C++ BuilderX, Dec-C++ 4 & 5, and Code::blocks.
I started out with Dev-C++, it's quite user friendly and quite easy to use. I reccomend using Code::blocks after you get friendly with the language, and need(or want) to do more advanced stuff.


Dev-C++
http://prdownloads.sourceforge.net/dev-cpp/devcpp-4.9.9.2_setup.exe

Code::Blocks
http://downloads.sourceforge.net/codeblocks/codeblocks-8.02-setup.exe

Part 3
Starting the program:
I am not going to cover installing the programs themself in full, that would be long and most people know that next is your best friend.
After you have installed Dev-C++ or Code::Blocks (or even both!) open it up. Now we basically need to make a new file, where we will type our code in.

For Dev-C++
File -> New -> Source file Or just push ctrl+n

For Code::Blocks
File -> New -> Empty file Or just push ctrl+shift+n


Now we are with a blank page.

Part 4
The making of the program:
First we are going to the this on your first line. EXACTLY how I have it:
#include <iostream>
This tellsthe compiler to go grab the file called iostream and smack it on top of your program! Don't worry about this yet, but you need it!
On the next line, type exactly how I do:
using namespace std;
This is so we don't need to type in as much code (don't worry! same result!)
Next line:
int main () {
This tells the program to start here. When the user starts your program, it will start at int main.
Next line:
cout << "Hello Dodian world!!!" << endl;
This basically says, on the command prompt, using the out command, display Hello Dodian world! The endl tells the program that it should start a new line on the command prompt.
Now, we're almost done, but we need something to "stop" the program, make it wait until the user has finished reading it, and then exit the program by typing the "any" key (nerd joke :p)
The next line should be:
system("PAUSE");
This runs a batch command, the pause command!!! Weee!
Finally, the last line of the program:
return 0; }
That tells the program to exit. Now, if you copied my instructions, your code should appear EXACTLY like this:
#include <iostream>

using namespace std;

int main() {
cout << "Hello Dodian world!" << endl;
system("PAUSE");
return 0; }

Now, to run our program!

For Dev-C++:
Execute -> Compile
If it shows the message Done then you know you typed your program correctly!
Now Execute -> Run

For Code::Blocks
Build -> Build
It should show in blue near the bottom something like this:
Compiling: C:\Documents and Settings\Adam\My Documents\CPP\Untitled1.c
Linking console executable: C:\Documents and Settings\Adam\My Documents\CPP\Untitled1.exe
Process terminated with status 0 (0 minutes, 8 seconds)
0 errors, 0 warnings
If it shows 0 errors, then you typed everything correctly!
Build -> Run


Part 5
Conclusion:
See? That wasn't a bunch of 1's and 0's!!! That wasn't too hard either! ^_^
Hopefully someone else gets the incentive to start programming in C++, and goes on to become a successful game or program developer. (Remember, over 70% of games such as Quake are made in C++!)
If any of you have any problems or thanks please let me know.

This is 9000234 going out.
Over!!!

Dangt351
11-15-2008, 01:22 AM
Nice Guide.

~Dangt351

pure4lyf
11-15-2008, 01:24 AM
Good guide =] Very confusing for people that don't know what they're doing.

~Pure4lyf

Arsenal
11-15-2008, 02:13 AM
Ok guide. =] If you make a next guide, teach people on how to make a calculator in DOS form. Here's mine.
#include <iostream>

using namespace std;

int a;
//This is a GLOBAL VARIABLE because it is outsite the main()
int main(){

int b;
int c;
cout << "This calculator was made by Arsenal94. ""\n";
cout << "Please notify me on 'arsenal1990666@hotmail.com' if you see this leached. ""\n";


cout << "Press 1 to do addition""\n";
cout << "Press 2 to do subtracting""\n";
cout << "Press 3 to do division""\n";
cout << "Press 4 to end the calculator""\n";
cout << "Enter a choice: ";
cin >> a;

switch (a) {
case 1:
cout << "Please enter a first number to add.: ""\n";
cin >> a;
cout << "Please enter a second number to add.: ""\n";
cin >> b;

c = a+b;

//what this means is that it is plussing the Avariable and the Bvariable together.
cout << "The total of the two numbers is: " << c << "\n";

//where it has << c << is means to insert what 'c = a+b' answer is. \n means new line.
case 2:
cout << "Please enter a first number to minus: ""\n";
cin >> a;

cout << "Please enter a second number to minus: ""\n";
cin >> b;

c = a-b;

cout << "The Total of the two numbers is: " << c << "\n";

case 3:
cout << "Please enter a first number to divide: ""\n";
cin >> a;

cout << "Please enter a second number to divide: ""\n";
cin >> b;

c = a/b;

cout << "The Total of the two numbers is: " << c << "\n";

case 4:
cout << "Thank you for using the calculator created by Arsenal94.""\n";





system("pause");}
}
//when 'system("pause");' is there, it means it has stopped and the user needs to hit any key to continue.
Made it like 6months ago. All the comments were there to help me remember stuff. XD
Also, I know there are a lot of things wrong with it. XD Don't have to point them out though. =P It was the very first thing I ever did with C++. I'll show you my ping-pong game with C++ and Allgero game library later.

I'll give this tutorial 7/10. Needs to be a bit better but it teaches the very basics which is essential.

Boylight
11-15-2008, 02:52 AM
You understand the language English to at least a grade 5 level




Okay,glad that I am over it.
Overall 6.5/10 for guide.

ToshiXZ
11-15-2008, 01:33 PM
Ok guide. =] If you make a next guide, teach people on how to make a calculator in DOS form. Here's mine.
#include <iostream>

using namespace std;

int a;
//This is a GLOBAL VARIABLE because it is outsite the main()
int main(){

int b;
int c;
cout << "This calculator was made by Arsenal94. ""\n";
cout << "Please notify me on 'arsenal1990666@hotmail.com' if you see this leached. ""\n";


cout << "Press 1 to do addition""\n";
cout << "Press 2 to do subtracting""\n";
cout << "Press 3 to do division""\n";
cout << "Press 4 to end the calculator""\n";
cout << "Enter a choice: ";
cin >> a;

switch (a) {
case 1:
cout << "Please enter a first number to add.: ""\n";
cin >> a;
cout << "Please enter a second number to add.: ""\n";
cin >> b;

c = a+b;

//what this means is that it is plussing the Avariable and the Bvariable together.
cout << "The total of the two numbers is: " << c << "\n";

//where it has << c << is means to insert what 'c = a+b' answer is. \n means new line.
case 2:
cout << "Please enter a first number to minus: ""\n";
cin >> a;

cout << "Please enter a second number to minus: ""\n";
cin >> b;

c = a-b;

cout << "The Total of the two numbers is: " << c << "\n";

case 3:
cout << "Please enter a first number to divide: ""\n";
cin >> a;

cout << "Please enter a second number to divide: ""\n";
cin >> b;

c = a/b;

cout << "The Total of the two numbers is: " << c << "\n";

case 4:
cout << "Thank you for using the calculator created by Arsenal94.""\n";





system("pause");}
}
//when 'system("pause");' is there, it means it has stopped and the user needs to hit any key to continue.
Made it like 6months ago. All the comments were there to help me remember stuff. XD
Also, I know there are a lot of things wrong with it. XD Don't have to point them out though. =P It was the very first thing I ever did with C++. I'll show you my ping-pong game with C++ and Allgero game library later.

I'll give this tutorial 7/10. Needs to be a bit better but it teaches the very basics which is essential.
Yeah, thinking about doing loops and if statements next, so a number guessing game. And yah, your calculator is missing a few things xD

Arsenal
11-15-2008, 05:09 PM
Yeah, thinking about doing loops and if statements next, so a number guessing game. And yah, your calculator is missing a few things xD
A few? Lol. More like a thousand things. XD

Kyle
12-03-2008, 10:23 PM
Nice, I take C++ at school, we use Imacs for computers, and we use Terminal and EMACS to do C++. It's pretty cool, it can be quite boring sometimes though =\
~Skiller

Underdog5
04-21-2009, 08:35 AM
Thanks alot, that really got me started in the direction I needed!

4evatillnoen
07-31-2009, 11:57 AM
Nice Guide.

Nottz
07-31-2009, 01:48 PM
Nice Guide.

Posting on really old topics is called gravedigging, and it's against the rules. I'm not sure if it applies to tutorials, however, so I won't infract you. Just don't do it again.