How to Make a Simple Addition Calculator Using C++
Find a compiler to use.Figure out a compiler that you would use, a compiler is an engine that will run your code. For this simple program, you can use Visual Studio, which is a good compiler for most coding languages on PC and Mac. It can be found here for free at .
Start a project in Visual Studio.After installing Visual Studio, go to the top bar and hit , thenNew Project, thenProject.
Choose the type of program you want to build.A window will pop up, choose theVisual C++option, thenEmpty Project.
- Make sure to rename your program to whatever you want in the “Name:” section; also change the location of the program to desktop in order to be able to find it.
Add the source file.The source file is basically the heart of the application; it contains all the code you will be writing, and it will contain what the application will read.
- You should be able to see a tab on the right of the page. If not, click on on top of the page and click onSolution Explorer. After that, right click on theSource Filesfolder in the solution explorer on the right. Then , andNew item. A window will pop up; click on .
Writing Your Code
Add libraries.Think of libraries as tools your program needs in order to do certain things; when you add these, your program will be able to know how to do simple stuff such as add numbers to more complicated functions. For this program, you will be using these libraries.
#include <conio.h>// For function getch() #include <cstdlib> // For several general-purpose functions #include <fstream> // For file handling #include <iomanip> // For formatted output #include <iostream> // For cin, cout, and system #include <string> // For string data typeusing namespace std; // So "std::cout" may be abbreviated to "cout"
- Make sure to copy these exactly. Anything after the “//” is a comment statement; they basically explain what this library does. For instance, “<conio.h>” is used to use the function “getch()”. Without that library, you would not be able to use that function in the program.
- The program will ignore everything after the “//” for it knows they are comments for you, the coder. You do not have to type comments. They just help you to remember what these functions do.
- Any comments should show up in green in Visual Studio, indicating that the compiler will not read that line of code.
Add your main function.You will now add the main function that will contain the program. Type the following:
- Everything between those two brackets will be your code for the calculator.
- Make sure your code is in between those brackets.
Add a welcoming message to your application.When your calculator starts, you will want to greet your users with a message! The following piece of code will print out a note for the user. These “cout” statements are used to print out an output in a program.
"Type message here"endl
- Where it says” Type message here” write down your message; for instance, “Welcome to simple calculator!” would be a good one. Make sure your code looks exactly like above.
Declare your variables.In a simple addition calculator, there are three variables: the numbers that are being added to each other and the sum of these two. For the sake of simplicity, you will use “x” and “y” as your numbers added and “z” as the answer. “x” and “y” will be entered by the user, and your program will calculate the result which is “z”. In order to use these variables, you would have to declare them in C++. To do that type as follows:
- “int” stands for integer, a variable type that can be used in C++. You will later declare the result variable once you have received the input from the user.
Get input from the user.Now to ask the user to put in the numbers to be summed by out calculator! In order to do that you use a “cout” statement again, but this time you also use a “cin” statement which will signify a use of input. In order to do this, write the following code:
"Enter the first integer: "endl x "Enter the second integer: "endl y
- Make sure you use “>” for "cin".
- These lines of code will ask the user for the two numbers he wishes to be summed; the cin will assign the numbers entered to “x” and then to “y”.
Sum the inputs.Now you will do the calculations; luckily this isn't too complicated, and computers are capable of doing very complicated math problems. All you have to do is declare the answer “z” as the sum of “x” and “y”.
- What this does is initialize “z” as the sum, and the computer will do the math by itself.
Output your answer.Use another “cout” statement in order to output the answer. Write the following code to output the answer:
x y z endl
- Make sure you type the code exactly as shown.
Add a farewell statement.Output a statement thanking the user for using the program, and then use the “_getch” function to prompt the user to press anything to close the program, otherwise, the program will close instantly after doing the calculation. Write the following code:"Thank you for using Simple addition calculator!"endl "Press any key to exit the program..."endl _getch
Testing the Application
Run the application.To test out the program, click theLocal Windows Debuggerbutton on the top bar. When you click it, a popup should come up. Hit unless it says build error and asks you try to revert to the previous build. If that happens, press to fix your code; make sure the code you copied is identical to what’s shared here.
- If the program works, a console should pop up and your application should run! Congratulations you just built your first C++ application, and a calculator at that!
- Make sure you save your code by clicking on . and .
Find your application.Now that you’ve built your program, you can run it instantly using the execution file without having to open Visual Studio every time.
- To do this, after saving, close Visual Studio and find the file you saved. This would be where you put the location of the program in the beginning, preferably the desktop to find it instantly).
- Open your project folder, go to DEBUG and you should find the .exe file with the name of the program you made; double-clicking that will run your application.
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
- Do not forget to use semi-colons after certain pieces of code.
- Make sure you have the exact same code as shown; otherwise the code will not work.
- Copy the code slowly and patiently in order not to make any mistakes that would cause errors.
Video: Visual Basic .NET Tutorial 4 - How to Create a Simple Calculator in Visual Basic
Peter Gordon’s Crumbed Camembert With Apple And Mango Recipe
How to Make a Pinwheel
I Tried Wearing Eyebrow Wigs And Heres What Happened
8Signs That Your Body IsCrying for Help
Treat Your Mama Right This Holiday With These GiftIdeas
Karen Millen teams up with Step Up Club to empower women
How to Make Christmas Eve As Special As Christmas
Marie Claire meets David Cameron
Street Style Star Aimee Song Shares Her Iconic Must-Haves
How to Talk to God Through Prayer
The 20 Best Stouts in the World
How to Make a Bouillabaisse
Date: 06.12.2018, 20:00 / Views: 91251