Before I begin, I'll just say that I don't know where you're at as far as skill on computers. I might miss a step, and I cannot immediately correct a mistake if you happen to make one. So, if you're having any trouble with this, please PM me. Also, I realized when I was right about finished, that I totally ignored OS X users. If this affects you, pretend you use Linux, and PM me any problems.
The first step of using C++ is installing the compiler. I use MinGW in Windows, and gcc in Linux.
For Windows:
It might be a good idea to read this
introduction to MinGW.
Then, follow the instructions
here.
For Linux:
Open up a terminal. If this is your first time doing this, welcome (it's dark in there, isn't it?)
type
and press Enter. Hopefully, it gives you a block of text describing the compiler version. If it gives you a message along the lines of "not found," you'll have to install the compiler. Consult the documentation for your distribution. Or just perform a search of "[distribution name] gcc". I'm sure it will pop up. Follow the instructions there (it will probably be an installation similar to others on your distribution).
Ok, hopefully that step went ok. That's probably the hardest part of this spoiler section. Let's move on to a quick program.
Let's keep things neat. It's a good idea to make at least one folder for your programs. Call it whatever you want. "Programs" is a nice name. As you make more complicated programs, you should give them dedicated folders. But simple programs can be put in the same directory as long as it isn't too unorganized.
Ok, open up your favorite text editor. If you need one, I would recommend Notepad++ for Windows, geany as a minimal Linux IDE, gedit for Gnome, or kate for KDE. But try them all out. If you're in a hurry, you can even use wordpad or notepad. Just as long as "Office" isn't anywhere in the name. Here's the code:
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
}
Save it as "hello.cpp" in your programs folder.
A quick note: C++ generally doesn't care about white space. You don't need to match indentation or anything like that.
First line: #include <iostream>
This pulls in the Input/Output Stream library. This gives the program access to "cout," which will allow us to write to the screen.
Second line: using namespace std;
This line makes the program assume that we are in the namespace "std". Now, we don't have to reference std every time we want to use something within it. Don't worry, we'll get there. Notice the semicolon. Statements use semicolons to end. I think it's hard at first to recognize when to use semicolons and when not to use semicolons.
Third line: int main()
main is the main function of every C/C++ program. This is where the program begins to run. The "int" at the beginning of the line says this function returns a number. We'll see this later. The left curly bracket on the next line tells us the function has begun. The right curly bracket at the end tells us the function has ended. Do not get parentheses and curly brackets confused. You'll get an error, and it will be just about impossible to see (I know someone who did this).
Fifth line: cout << "Hello World" << endl;
This line tells the program to print the string "Hello World" to the screen. Imagine that cout is the screen, then follow the arrows. You can also see that "endl" gets passed to cout. This stands for "endline." It's the equivalent of hitting Enter in a text area.
return 0;
This statement tells the function (main) to finish and passes a 0 up (in this case, to the operating system). This will get explained more when functions are discussed.
Ok, let's compile it. Open the Terminal/Command Prompt (under accessories in the start menu in Windows). Navigate to the necessary directory. To enter a folder, type
To exit a folder, type
Alright, assuming we're in the right directory, you type
Hopefully you don't get any errors. If you do, PM me. If you get an error that g++ cannot be found, and you are
sure you installed it, you might have a path problem (this would most likely be a Windows problem). I won't clutter this with the solution if no one has the problem.
Assuming the program compiled, you get to run it. From the same folder, type "./a.out" if you're using Linux or "a.exe" if you're using Windows. "Hello World" should print onto the screen. If it doesn't (and you're using Windows) check the directory for a text file called "std.out". MinGW might be configured to print it there. You can open it in notepad.