How to Create a Simple Program in C++: 5 Steps

Table of contents:

How to Create a Simple Program in C++: 5 Steps
How to Create a Simple Program in C++: 5 Steps

Video: How to Create a Simple Program in C++: 5 Steps

Video: How to Create a Simple Program in C++: 5 Steps
Video: Master the Rugby Tackle with These 3 Expert Tips | Dominate the Field Now 2024, December
Anonim

Do you want to program in C++? The best way to learn is to pay attention to examples. Look at the basic C++ programming schematic to learn about the structure of a C++ program, then build a simple program yourself.

Step

Step 1. Set up the compiler and/or IDE

Three good choices are GCC, or if your computer is running Windows, Visual Studio Express Edition or Dev-C++.

Step 2. Try some sample programs

Copy and paste the following code into a text/code editor:

This simple program is provided by Bjarne Stroustrup (C++ developer) to check your compiler:

#include

  • Program to find the result of adding two numbers:

    
    
  • Program to find the result of multiplication:
  • Program to find the exponential value:
  • Step 3. Save this file as a.cpp file with a name that reflects the program

    Don't get confused, there are many other extensions for C++ files, choose one (eg *.cc, *.cxx, *.c++, *.cp).

    INSTRUCTIONS': If the Save as Type option appears: {select "All Files"}

    Step 4. Compile

    For Linux users and gcc compiler, use Command: g++ sum.cpp. Windows users can use any C++ compiler, such as MS Visual C++, Dev-C++ or other program of choice.

    Create a Simple Program in C++ Step 5
    Create a Simple Program in C++ Step 5

    Step 5. Run the program

    For Linux users and gcc compiler

    Command:./a.out (a.out is the executable file generated by the compiler after program compilation.)

    Tips

    • cin.ignore() prevents the program from ending prematurely and closes the window instantly (before you can see it)! Press any key if you want to end the program. cin.get() works in a similar way.
    • Add // before all comments.
    • Don't be afraid to experiment!
    • For more details on programming with C++, visit cplusplus.com.
    • Learn C++ programming with ISO standards.

    Warning

    • Your program will crash if you try to enter an alphabetical value in one of the "int" variables. Because there is no error handling, your program cannot change the value. It's best to use a string or throw an exception.
    • Avoid Dev-C++ as much as possible as this software has a lot of bugs, the compiler is out of date and hasn't been updated since 2005.
    • Never use expired code.