How to Create a Simple Program in C++

Get a compiler and/or IDE., Try some example programs., Save this as a .cpp file with a name that accurately reflects your program., Compile it., Run the program.

5 Steps 1 min read Medium

Step-by-Step Guide

  1. Step 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++. , Copy and paste the following into a text/code editor:
    A simple program is given by Bjarne Stroustrup (developer of C++) to check your compiler: #include <iostream>] #include <string> using namespace std; int main () { string s; cout << "Your Name \n"; cin >> s; cout << "Hello, " << s << '\n' ; return 0; } A program for finding the sum of two numbers: ] #include <iostream> using namespace std; int main () { int no1, no2, sum; cout << "\nEnter the first number = " ; cin >> no1 ; cout << "\nEnter the second number = " ; cin >> no2 ; sum = no1 + no2 ; cout << "\nThe sum of "<< no1 <<" and "<< no2 <<" = "<< sum <<'\n' ; return 0 ; } A program for finding the product in multiplication problems: ] #include <iostream> int main() { int v1, v2, range; std::cout <<"Please input two numbers:"<< std::endl; std::cin >> v1 >> v2; if (v1 <= v2) { range = v2
    - v1; } else { range = v1
    - v2; } std::cout << "range = " << range << std::endl; return 0; } A program for finding the value of exponents: ] #include <iostream> using namespace std; int main() { int value, pow, result=1; cout << "Please enter operand:" << endl; cin >> value; #cout << "Please enter exponent:" << endl; cin >> pow; for (int cnt=0; cnt!=pow; cnt++) result*=value; cout << value << " to the power of " << pow << " is: " << result << endl; return 0; } , 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"} , 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., For users of Linux and gcc compiler Command : ./a.out (a.out is an executable file produce by compiler after compilation of program.)
  2. Step 2: Try some example programs.

  3. Step 3: Save this as a .cpp file with a name that accurately reflects your program.

  4. Step 4: Compile it.

  5. Step 5: Run the program.

Detailed Guide

Three good choices are GCC, or if your computer is running Windows, Visual Studio Express Edition or Dev-C++. , Copy and paste the following into a text/code editor:
A simple program is given by Bjarne Stroustrup (developer of C++) to check your compiler: #include <iostream>] #include <string> using namespace std; int main () { string s; cout << "Your Name \n"; cin >> s; cout << "Hello, " << s << '\n' ; return 0; } A program for finding the sum of two numbers: ] #include <iostream> using namespace std; int main () { int no1, no2, sum; cout << "\nEnter the first number = " ; cin >> no1 ; cout << "\nEnter the second number = " ; cin >> no2 ; sum = no1 + no2 ; cout << "\nThe sum of "<< no1 <<" and "<< no2 <<" = "<< sum <<'\n' ; return 0 ; } A program for finding the product in multiplication problems: ] #include <iostream> int main() { int v1, v2, range; std::cout <<"Please input two numbers:"<< std::endl; std::cin >> v1 >> v2; if (v1 <= v2) { range = v2
- v1; } else { range = v1
- v2; } std::cout << "range = " << range << std::endl; return 0; } A program for finding the value of exponents: ] #include <iostream> using namespace std; int main() { int value, pow, result=1; cout << "Please enter operand:" << endl; cin >> value; #cout << "Please enter exponent:" << endl; cin >> pow; for (int cnt=0; cnt!=pow; cnt++) result*=value; cout << value << " to the power of " << pow << " is: " << result << endl; return 0; } , 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"} , 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., For users of Linux and gcc compiler Command : ./a.out (a.out is an executable file produce by compiler after compilation of program.)

About the Author

J

Judith Rogers

Specializes in breaking down complex cooking topics into simple steps.

77 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: