How to Make a Simple C++ Calculator
by KalebTringale in Workshop > Tools
2016 Views, 0 Favorites, 0 Comments
How to Make a Simple C++ Calculator
These instructions portray how to write a simple program in C++. Programming is a valuable skill, and everyone should try it at some point. Programming offers excellent problem-solving techniques and skills, and the logic is very mathematical.
Getting Set Up
When making an application, start by choosing an Integrated Development Environment, or IDE. This is where all of the code for the application will be written. Many popular IDEs are Visual Studio, NetBeans, or Sublime Text Editor. This example will be written in Visual Studio 2019, which can be downloaded at https://visualstudio.microsoft.com/ After choosing your IDE, your language of choice needs to be determined. A lot of people will use C++ or python because they are relatively simple to start with. Any language can be used, some languages differ slightly, but the logic is all the same. This example will showcase C++ syntax.
Creating a Project in Visual Studio
Now that everything is set up, start by creating a blank console c++ application in Visual Studio. To do this, click the "Create new project" button, and search for C++ Console App. Name the project, then push the "Create" button. Once the blank project is created, we need to locate the main method. The main method, usually identified as “int main()”, is a function that will perform a set of instructions given to the computer. All of the code for this application will be written in the main method.
Display to the Console
Now that the main method has been identified, the computer needs to be given instructions so it knows what to do. When the application is run, it should print out a title message, and prompt the user. To do so, start by typing
cout << "Welcome to the Calculator";
"cout" is short for console out, it is a function included in the standard c++ library, that will output whatever we give it to the screen. In this case, it will print “Welcome to The Calculator”. In C++, make sure every line of code you write ends with a “;”.
Initialize Variables
After displaying to the console, create a variable to store data that the program can access and manipulate. Just like in regular math, a variable can hold almost any value and can have almost any name. To declare a variable in C++ it needs a data type, a name, and a value. Start by typing:
int x = 0;
This declares an integer, or whole number named x with a value of 0. The calculator will need to accept 2 variables, so create another one named y.
Getting User Input
After initializing the variables, they need to be assigned a value by the user. To do that we have to use a function called cin. cin will take text that a user inputs, and store it inside a variable. First, prompt the user by typing:
cout << "Enter a number";
Next store the input with cin by typing:
cin >> x;
Create another prompt and do the same thing for variable y.
Calculating the Total
After obtaining and storing input from the user, it’s time to calculate the variables and get a total variable. Start by declaring a new integer variable called total, this time set total equal to the first variable plus the second variable. If done correctly it should look like:
int total = x + y;
Display Values to Console
Finally, after obtaining all of the values, and finding the total, it’s time to output everything to the console. To do this we are going to use cout again. Start by saying:
cout << x << “ + ” << y “ = “ << total;
By using this format, our console output should say something like “10 + 12 = 22”. Now when run it, the application will accept any 2 whole numbers and calculate the addition.