How to build a program in C++
Building a program is all about creating the program and executing it successfully. There are some steps precisely, which must be followed to make the program.
Step 1: Get an IDE or a compiler. GCC, Visual Studio Express Edition, or Dev-C++, if your computer runs Windows, are three excellent options.
Step 2: Try out some sample programs.
Example 1
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string s;
cout << "Your Name \n";
cin >> s;
cout << "Hello, " << s << '\n' ;
return 0;
}
Output
Your Name
John
Hello, John
Example 2: A program to find 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 ;
}
Output
Enter the first number = 5
Enter the second number = 8
The sum of 5 and 8 is = 13
Example 3: A program to find a range of given numbers.
#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;
}
Output
Please input two numbers:
4 6
range = 2
Example 4: A program to find 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;
}
Output
Please enter operand:
2
Please enter exponent:
5
2 to the power of 5 is: 32
Step 3: This should be saved as a a.cpp file with a name that correctly describes your application. Choose any of the many other extensions for C++ files, such as *.cc, *.cxx, *.c++, or *.cp, to avoid confusion.
Observer: It ought to read Save as Type: Choose "All Files."
Step 4: Assemble it. Use the command: g++ a sum.cpp if you're using Linux and the gcc compiler. Any C++ compiler, including MS Visual C++, Dev-C++, and other applications of their choice, can be used by Windows users.
Step 5: Run the application. Command:./a.out for Linux and gcc compiler users (a.out is an executable file produced by the compiler after the compilation of the program.)
Now let’s see the process of building a program in Visual Studio Code:
In Visual Studio, how to start a C++ project
- Select File> New > Project from the main menu to access the Create a New Project dialogue box.
- Set Language to C++, Platform to Windows, and Project type to Console at the top of the dialogue.
- Select Console App, then select Next, from the list of project types that have been filtered. Enter the project's name and, if desired, the project's location on the following page.
- To start the project, select the Create option.
Insert a fresh source file
- Click Solution Explorer from the View menu if it isn't already selected.
- The following source file should be added to the project.
- Right-click the Source Files folder in Solution Explorer, select Add, and then choose New Item.
- Click C++ File (.cpp) in the Code node, give the file a name, and then click Add.
- The.cpp file is opened in the Visual Studio editor and can be found in the Source Files folder in Solution Explorer.
- Type a legitimate C++ program that makes use of the C++ Standard Library in the file's editor or copy one of the sample programs and paste it there.
- File saving.
- Click Build Solution from the Build menu.
Internal Process Building
The compilation stages described are combined into a process called building in an IDE like Visual Studio. Building and then debugging software is a common approach.
According to how well we coded since our last build, the build produces the executable by compiling and linking the code or a list of problems. The executable File generated by Visual Studio will be launched when we select Start Debugging.
Programming is the process of creating programs. So you do not create programming. You create programs, and programs are the collection of step-wise instructions.
The program you want to create in C requires a code editor and compiler. You can use Code blocks, Turbo C ++, and Dev C++.
Conclusion
You can write an operating system or device driver, or embedded application. Also, by using C/++, you can build a desktop application, a mobile application, a web application, or a server application. Furthermore, you can write an enterprise business application like ERP or CRM and a video game or application for high-performance graphics.