Code Your First Program in C++
Coding in C++ can be very simple once you get the basics of it down. When you begin to learn C++ you are taught to code one of many fundamental programs, the most popular being the "Hello World!" prompt.
Steps
- Make sure you have the necessary tools. You need either a text editor and C++ compiler or an IDE that has both these things in one program. Many IDEs are specialized for a single programming language, so make sure to choose one that supports C++ (for example Microsoft Visual C++).
- Begin a new project. Launch your IDE or text editor. In Visual C++, start a new project by pressing File → New → Project → Empty Project. If you are using are planning to use a text editor, create a new directory on your computer and change into it.
- Create a new C++ (.cpp) file. In Visual C++, right click Source Files and add a new item, select cpp file. In many text editors, a new file is created by pressing Ctrl+N and then saving that file (don't forget to use a descriptive name, like helloWorld.cpp).
- Include the iostream library. Begin your code with #include <iostream>. This will allow you to display text and to read the user's input with the cin and cout expressions.
- Tell the program to use the "std" namespace. Type the line using namespace std;. Since you included a standard library, you would have to preface each mention of cout and cin with std::. Always end a line of code with ';' to let the compiler know that the line is finished.
- Declare the main function. Functions always have a return type, and it is a convention in C-style languages that the main function returns an integer. So begin your function with the line int main(){ and ending it with a closing curly bracket } . This is where your code will go inside.
- Output "Hello World!". This is done with the line cout << "Hello World!" << endl;, which belongs into a new line between the opening and the closing curly bracket. The 'cout' command tells the console to output something, in this case the "Hello World!" text. The endl at the end tells the program to start whatever text you output next next on a new line. You could also have used "Hello World!\n" as your string and dropped the << endl part. Don't forget to end this line with a semicolon (";").
- Stop the program from automatically closing right after it opens. Skip this step if you are only going to run the program from an IDE or the command line. When you compile the program to an .exe file, you won't be able to see the output without this step, since the program is so fast. Make the program close on user input instead. Type cin.get(); to get user input. After that, write return 0;. It is convention that the main function should return 0 if there were no errors, and a number other than 0 if there was an error. Returning a value makes the function finish. So typing any valid code after a return expression won't have any effect.
- Check your code. Your entire code should now look like this: Template:CodeBox
- Run the code. If you used a text editor, save now and compile your code. For compiling, you can follow Compile-a-C-Program-Using-the-GNU-Compiler-(GCC), but use g++ instead of the gcc command. Then run the resulting file, which will usually be called either a.out or something with the .exe. In and IDE, simply press the correct button, which often says something like "Run Program".
Tips
- While even a very simple text editor (like Notepad) is enough to write a C++ program (as long as you have a compiler to compile it), when you write longer and more complex programs, it will become hard to tell what is where in the code. When that happens, consider getting a text editor with syntax colouring (like Vim). This will make it easier to distinguish code parts from each other.
- C++ doesn't care with how many spaces or tabs you indent your code, or whether your way of indentation is consistent across the code. You are even allowed not to indent your code at all. But it's better to indent your code in a consistent manner, because it is hard to read otherwise.