Create a Simple Program in C++

Ever wanted to program in C++? The best way to learn is by looking at examples. Take a look at the basic C++ programming outline to learn about the structure of a C++ program, then create a simple program on your own.

Steps

  1. Get a compiler and/or IDE. Three good choices are GCC, or if your computer is running Windows, Visual Studio Express Edition or Dev-C++.
  2. Try some example programs. Copy and paste the following into a text/code editor:
  3. A program for finding the sum of two numbers:
  4. A program for finding the product in multiplication problems:
  5. A program for finding the value of exponents:
  6. Save this as a .cpp file with a name that accurately reflects your program. Don't confuse there are many other extensions for C++ files, choose any of them (like *.cc, *.cxx, *.c++, *.cp) .
    • HINT': It should say Save as Type: {select "All Files"}
  7. Compile it. For users of linux and gcc compiler, use Command : g++ sum.cpp. Users of Window can use any C++ compiler, such as MS Visual C++,Dev-C++ or any other preferred program.
  8. Run the program. For users of Linux and gcc compiler
    Command : ./a.out (a.out is an executable file produce by compiler after compilation of program.)



Tips

  • Add // before all of your comments.
  • cin.ignore() prevents the program from ending prematurely and closing the window immediately (before you have time to see it)! Press any key if you want to end the program. cin.get() functions in a similar manner.
  • Feel free to experiment!
  • For more details about programming in C++ give a visit cplusplus.com
  • Learn programming in C++ with ISO standards

Warnings

  • Your program will crash if you try to input alphabetical values to one of the "int" vars. Since no propper error trapping is done your program can't convert the values. Better read string or catch your exceptions.
  • Make sure to stay as far away from Dev-C++ as possible, because it has multiple bugs, an outdated compiler, and has not been updated since 2005.
  • Never use obsolete code.

Things You'll Need

  • A text/code editor (e.g. vim, notepad, etc).
  • A compiler.
  • Alternatively, an IDE contains an editor and a compiler.
  • Turbo c
  • Codepad online
  • Notepad++

Related Articles