• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Tutorial: C++ for beginners

Status
Not open for further replies.
Level 8
Joined
Jul 3, 2004
Messages
334
First, you will need a good compiler to start programming.

Get this, DEV-CPP.
http://ftp1.sourceforge.net/dev-cpp/devcpp4.zip



Sections:
Tutorials (Lessons)
FAQ (NEW!)
Other, Untested Compilers (NEW!)
Extra (NEW!)



---LESSON 1---
This is the best compiler that I have found. Install it and exit the program. Start up notepad.exe in the accessories, or create a new one on your hard drive. Open it up now. Copy and paste the following text to your notepad file.

Code:
#include <iostream>

using namespace std;

int main()
{
	cout<<"Hello World!\n";
	cin.get();

}

Rename the text file as "hello.cpp". Double click on your new file. You should now be in "DEV-CPP". Hit "F9" on your keyboard. This will compile and run it. You should have a window that will pop up and say "Hello World!". Let's disassemble the coding now. The { and } mean beginning and end. You will use this for ALL separate functions. #include <iostream> is the function that it uses to call strings and stuff like that. The using namespace std; means..well..I don't know YET. The int main means that this is the "main function". Int declares the variable "main". cout<<"Hello World!\n"; makes the string appear. \n means to start a new line in the program. cin.get(); is a VERY important function. This keeps the window open. Try running it without this command. I haven't explained the ; yet. This is required for many lines in the C++ language. I don't know why, but it seems a little stupid to me. That is it about this program.
---END LESSON 1---



---LESSON 2---
Declaring variables is easy. Go into your compiler\text editor and write:
Code:
#include <iostream>

using namespace std;

int main()
Now, get ready for your next program: returning an integer variable that has been entered in line 1. (An integer is a number) Copy and paste this code over your existing code, or type it all out on your own:
Code:
#include <iostream>

using namespace std;

int main()
{
    int Number;
    cout<<"Enter a number: ";
    cin>> Number;
    cin.ignore();
    cout<<"You entered: "<< Number <<"\n";
    cin.get();
}
Compile and run the program. After that, let me explain the new lines of the program. Go back to lesson one if you don't know about "#include <iostream>", "using namespace std;", "int main()", and ";". "Int Number" declares your new integer integer "int". Remember the { and } means beginning and end. " Cout<<"Enter a number: "; " makes the text.
"Cin>> Number;" is puting your entered number after the prompt.(Enter a number: ) "Cin.ignore();" makes the program so that it doesn't exit after you hit enter like the "Hello World" program did.
" Cout<<"You entered: "<< Number <<"\n"; " makes your program so that it displays the text and your integer variable. "Cin.get()" makes the window stay until you hit enter. That's it to this lesson. Keep up to date on these tutorials!
---END LESSON 2---



---LESSON 3---
This is a very short lesson, so please bear with me.
You will need the code from last lesson, because this code comes from that lesson. Actually, I will just provide the code:
Code:
#include <iostream>

using namespace std;

int main()
{
    int Number;
    int Number2;
    cout<<"Enter a number: ";
    cin>> Number;
    cin.ignore();
    cout<<"Enter another number: ";
    cin>> Number2;
    cin.ignore();
    cout<<"You entered: "<< Number <<" "<< "and "<< Number2 <<"\n";
    cin.get();
}
F9 to compile and run. Do it. This is derived from the first integer returner. I want you to study this code. I did this extra part from scratch, and I'm sure that by using code already used, you can decode the function of this.
Like I said, short.
---END LESSON 3---



---LESSON 4---
This lesson is pretty short to. This is a very important thing to know as we progress to further lessons: comments. you can outline comments by using:
Code:
//Your comment text here 
or
/*Your comment text here
if you are using DEV-CPP, these comments will be in a light gray and italicized
Comments are important for making definitions of functions.
---END LESSON 4---



---LESSON 5---
Now that we have covered a few more areas, let's move on: next, we are going to create a calculator that can do all four functions: addition, subtraction, division, and multiplication. Use this code:
Code:
#include <iostream>

using namespace std;

int main()
{
    float num1;
	float num2;
	float add;
	float sub;
	float div;
	float mul;
/* --- Actions --- */	
	cout<<"Enter Number 1: ";
	cin>> num1;
        cout<<"Enter Number 2: ";
        cin>> num2;
        cin.ignore();
        add = num1 + num2;
        sub = num1 - num2;
        div = num1 / num2;
        mul = num1 * num2;
        cout<<"Your sum is: "<< add<< "\n";
        cout<<"Your difference is: "<< sub<< "\n";
        cout<<"Your quotient is: "<< div<< "\n";
        cout<<"Your product is: "<< mul<< "\n";
        cin.get();
}
Run this in whatever your compiler is. After that, lets disect the coding.
1. A float is an integer that has the ability to contain decimal points(.). This is useful, and should always be used in calculator programs for the integers.
2.Next is the comment to separate the variable declarations from the actions.
3.The string for prompting to enter the number.
4.The action for making the last entered text to be the value of the float variable.
5.The next two lines are repeats of the last two numbers. (3., 4.)
6.These four lines tell what the variables should contain next
7.The next four lines tell you the answers to the problem, it says the text then tells you the value of the variables.

That was a longer lesson, but it wasn't so bad, now was it?
---END LESSON 5---



---FAQ---

Q:Why do we need #include <whatever it is> ?
A:This is including the directive used for functions to work, for example:
#include <windows.h> is needed for the function "Sleep", which can be a VERY important function in some cases.

Q:Why bother with C++ now..?
A:If your going to school, you will probably eventually go to college. There, C++ is required to learn, unless you are too deeply interested in something else.

Q:I can't understand C++, help!
A:C++ is not in plain English, if you are having trouble understanding it, go get Visual Basic, and start from there.

Q:C++ is too boring, why does it have to be in a DOS type window?
A:If you go through all the necessary coding, you can change that.

---END FAQ---



---OTHER, UNTESTED COMPILERS---

Digital Mars C++ Compiler
ftp://ftp.digitalmars.com/Digital_Mars_C++/Patch/dm841c.zip
Also Requires: STL Port
ftp://ftp.digitalmars.com/Digital_Mars_C++/STL/stlport.zip


Intel C++ for Linux
http://www.intel.com/software/products/compilers/clin/noncom.htm

---END OTHER, UNTESTED COMPILERS---



---EXTRA---

I have things here that don't exactly fit in any other section.

DEV-CPP is now in Beta.Version 5!

---END EXTRA---


---TO BE CONTINUED---
 
Level 16
Joined
Sep 3, 2004
Messages
2,086
C++ is the text scripting for programmers. Simple C++ is also used to make certain JASS scripts as well. There are 2 things you can do:
1: go take computer lessons or go to computer camp
2: learn by making GUI triggers and converting them
3: Print it out and ask some "nerd" at your school to teach you how to do this, and apologize for laughing and mocking him
 
Level 8
Joined
Jul 3, 2004
Messages
334
GreyArchon said:
Aisde from the explanation what it -IS-, there's still a question remaining as to why this tutorial would be posted here? ;roll: Did someone request a C++ tutorial? Because in the end, most people here would prefer a JASS tutorial instead. You know, being a Warcraft community after all.

This is in the "Off Topic" section if you would have noticed. Any JASS tutorial would be in the JASS section or the tutorial section. No one requested this tutorial, it is here for the purpose of learning C++. If someone would learn this, then they could become a good tool writer for Warcraft 3. :wink:
 
Level 13
Joined
Jan 9, 2004
Messages
1,037
Err... this tutorial isn't very detailed. It seems you don't have too much experience yourself in c++ to be writing tutorials. If someone wanted to learn the language they would probably get a book or find some sort of tutorial site on it. A Warcraft III site will probably not get too many people interested in c++.
 
Level 23
Joined
Jul 18, 2004
Messages
1,862
oz02 said:
Err... this tutorial isn't very detailed. It seems you don't have too much experience yourself in c++ to be writing tutorials. If someone wanted to learn the language they would probably get a book or find some sort of tutorial site on it. A Warcraft III site will probably not get too many people interested in c++.

My thoughts exactly. Listen to this guy, Eusira and orcfan. He explains my opnion better than I do.
 
Status
Not open for further replies.
Top