×

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

  1. Click Solution Explorer from the View menu if it isn't already selected.
  2. The following source file should be added to the project.
  3. Right-click the Source Files folder in Solution Explorer, select Add, and then choose New Item.
  4. Click C++ File (.cpp) in the Code node, give the file a name, and then click Add.
  5. The.cpp file is opened in the Visual Studio editor and can be found in the Source Files folder in Solution Explorer.
  6. 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.
  7. File saving.
  8. 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.


Related Topics

Functions in C++ with Types and Examples

A function is a collection of statements that work together to complete a certain goal. It could consist of statements that execute repetitive operations or statements that conduct specialized jobs...

10 minutes read.

Diamond Pattern using While-loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

5 minutes read.

How to Reverse a String in C++ using While Loop

A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the condition no longer...

6 minutes read.

Learn C++ Tutorial

C++ Introduction C++ is an object-oriented programming language. It was developed by Bjarne Stroustrup at AT&T Bell Laboratories. It is superset (extension) of C programming language. Depending upon features supported by programming...

10 minutes read.

Print Table Using Do while Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

How to Reverse a String in C++ using For Loop

For Loop: We may loop through a certain section of C++ code repeatedly using the for loop. A for loop is carried out if the test expression yields a true result. The...

4 minutes read.

Octal to Decimal in C++

We need to write a system that converts octal number into equal decimal number when octal number is given as input. Let us look at an example of a program in...

2 minutes read.

Hello World Program in C++

The steps for “Hello World” C++ program are as follows: Write a C++ code given below in an editor. Save the file with .cpp Compile the code using C++ compiler or using online...

3 minutes read.

C++ Socket Programming

In this world, computer networking has become very important for sharing of data. Every good programmer has some knowledge about computer networking. Socket programming is one of the critical topics...

6 minutes read.

Armstrong Number using Do-While Loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

4 minutes read.

Static keyword in C++ vs Java

Both in C++ and Java, the static keyword is employed for essentially the same function. But there are some variations. The static keyword's similarities and differences between C++ and Java...

3 minutes read.

Hybrid Inheritance

C++ Hybrid Inheritance When more than one type of inheritance is combined in single inheritance is called as hybrid inheritance. C++ Hybrid Inheritance Example #include<iostream>   using namespace std;   class Student{       protected:           int rollno;       public:           void getRoll(int a){               rollno=a;           }           void putRoll(void){               cout <<"Roll No: "<< rollno<<endl;           }   };   class Test : public Student{       protected:           float subject1, subject2;       public:           void getMarks(float x, float y){               subject1=x;               subject2=y;           }           void putMarks(void){               cout<< "Marks gain: "<<endl <<"Subject1 =  "<<subject1<<endl<<"Subject2 = "<<subject2 <<endl;           }   };   class Sport{       protected:           float score;       public:           void getScore(float s){               score=s;           }           void putScore(void){               cout<<"Sports score: "<<score<<endl;           }   };   class Result : public Test, public Sport{       float total;       public:           void display(void);   };   void Result:: display(void){       total=subject1+subject2+score;       putRoll();       putMarks();       putScore();       cout<<"Total Score: "<<total<<endl;   }   int main(){       Result stu;       stu.getRoll(10);       stu.getMarks(40,50);       stu.getScore(60);       stu.display();       return 0;   } Output: Roll No: 10 Marks gain: Subject1 = 40 Subject2 = 50 Sports score: 60 Total...

1 minute read.

C++ Deque

Definition: Deque or the Doubly ended queue is a data structure or operation performed under queue where insertion and deletion are allowed at both ends. A deque is an ordered collection of...

5 minutes read.

Counting Frequencies of Array Elements in C++

We have an array of integer items with duplicate values, and our objective is to compute the frequencies of the different elements in the array. Methods: Methods that can be used to...

3 minutes read.

Pointer to Object in C++

What is a pointer? A pointer in C++ is used to point the variable by storing the address of the variable. In C++, to print the address of the variable, we...

4 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

C++ History

C++ is a middle-level programming language developed in 1980s by Bjarne Stroustrup at Bel Labs. C ++ development initially started in 1979, four years before its launch. It is started with...

1 minute read.

C++ Pipe Tutorial

A pipe is a mechanism for inter-process communication (IPC) in a Unix-like operating system. It allows two or more processes to communicate with each other by sending and receiving data...

3 minutes read.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.

Hierarchical Inheritance

C++ Hierarchical Inheritance Hierarchical inheritance inherits the property of one base class in more than one derived class.   C++ Hierarchical Inheritance Example #include <iostream>   using namespace std;   class Person {       char gender[10];       int age;   public:       void getPerson()       {           cout << "Age: "; cin >> age;           cout << "Gender: "; cin >> gender;       }       void dispPerson()       {           cout << "Age: " << age << endl;           cout << "Gender: " << gender << endl;       }   };   class Employee : public Person {       float salary;   public:       void getEmployee()       {           Person::getPerson();           cout << "Salary: Rs."; cin >> salary;       }       void dispEmployee()       {           Person::dispPerson();           cout << "Salary: Rs." << salary << endl;       }   };   class Student : public Person {       char level[20];   public:       void getStudent()       {           Person::getPerson();           cout << "Class: "; cin >> level;       }       void dispStudent()       {           Person::dispPerson();           cout << "Level: " << level << endl;       }   };   int main()   {       Person per;       Employee emp;       Student stu;       cout << "Student data" << endl;       cout << "Enter data" << endl;       stu.getStudent();       cout << endl << "Displaying data" << endl;       stu.dispPerson();       cout << endl << "Staff Data" << endl;       cout << "Enter data" << endl;       emp.getEmployee();       cout << endl << "Displaying data" << endl;       emp.dispPerson();   } Output: Student data Enter data Age: 10 Gender: f Class: 5 Displaying data Age: 10 Gender: f Employee data Enter data Age:...

1 minute read.