How to Code Your First Program in C++

Make sure you have a C++ compiler or IDE that you are comfortable with and prefer using., Launch your C++ compiler and began a new project., After having your empty project set up., Begin your code with #include this is to allow you to use the...

9 Steps 1 min read Medium

Step-by-Step Guide

  1. Step 1: Make sure you have a C++ compiler or IDE that you are comfortable with and prefer using.

    (Ex.

    Microsoft Visual C++)
  2. Step 2: Launch your C++ compiler and began a new project.

    In Visual C++ it's done by pressing File->New->Project->Empty Project. , Right click Source Files and add a new item, select cpp file. ,, Always end a line of code with ';' to let the program know that the line is finished. , This is where your code is going to go inside. , by typing "cout << "Hello World!" << endl;" The 'cout' command tells the console to output something, in this case its the "Hello World!" text.

    The 'endl;' at the end tells the program to start whatever code you have next on a new line. , This is done by making the program close on user input.

    Type "cin.get();" and then "return 0".

    This asks for an unspecified user input to end the program. 'cin' is the command used for user input. 'return 0' is to end the program without any errors. // the complete program follows: #include <iostream> using namespace std; int main ( int argc, char** argv ) // standard form ignore for now { cout << "Hello World!" << endl; cin.get(); return 0; }
  3. Step 3: After having your empty project set up.

  4. Step 4: Begin your code with #include <iostream> this is to allow you to use the cout(C-out) and cin(C-in) commands which are used to output and input from within the prompt.

  5. Step 5: Tell the program to use the "std" namespace by typing 'using namespace std;'.

  6. Step 6: Declare where your program codes by writing 'int main(){' and ending it with the closing brackets '}'.

  7. Step 7: Inside your 'int main() {' you will tell the program to output 'Hello World!'

  8. Step 8: After you have all that

  9. Step 9: you have to stop the program from automatically closing right after it opens.

Detailed Guide

(Ex.

Microsoft Visual C++)

In Visual C++ it's done by pressing File->New->Project->Empty Project. , Right click Source Files and add a new item, select cpp file. ,, Always end a line of code with ';' to let the program know that the line is finished. , This is where your code is going to go inside. , by typing "cout << "Hello World!" << endl;" The 'cout' command tells the console to output something, in this case its the "Hello World!" text.

The 'endl;' at the end tells the program to start whatever code you have next on a new line. , This is done by making the program close on user input.

Type "cin.get();" and then "return 0".

This asks for an unspecified user input to end the program. 'cin' is the command used for user input. 'return 0' is to end the program without any errors. // the complete program follows: #include <iostream> using namespace std; int main ( int argc, char** argv ) // standard form ignore for now { cout << "Hello World!" << endl; cin.get(); return 0; }

About the Author

J

Justin Wallace

Writer and educator with a focus on practical pet care knowledge.

47 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: