C++ Tutorial #3 – Making a Simple Calculator

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Welcome back programmers! Today I will be showing you to make a simple calculator. It will not look like the calculators you’ll find on your phones or computers. It will still be a console application. We’re going to be making console applications for a while, because that’s part of learning programming. Once you get past the boring stuff though, it will be a lot more fun when you start drawing images on the screen.

 

Lets get started on the boring stuff! 😛

 

Copy this code, and I’ll guide you through the steps:

 

 

This is what the program would look like:

 

calc

 

Here’s how our program works:

 

 

Most of you might know what “#include<iostream>” does, but I’ll explain for some of you who are still learning. It tells the program that we want to use the iostream header, which performs standard input output operations. It allows us to use “cout” an “cin” which I’ll explain later.

 

 

We include the conio.h header to use the “getch()” command, which I’ll explain when we get to the main function.

 

 

Basically means that we are using the “standard namespace”. It works as a shortcut. Without it, we would have to write “std::cout” instead of “cout”. I will cover more on namespaces in another post. As for right now, it’s not all that important.

 

 

This section of code is our main function. Our program begins inside the two braces {}. We add “getch();” to pause the program. I’m using “getch()” instead of “cin.get” in the previous tutorials, because it works better for me. “return 0;” is used to tell the program that our code ran successfully.

 

 

The two slashes tells the program that the rest of the line is a comment. comments don’t do anything to the program. You can just use it to take notes, or provide a small description of what that section of code does. “int number1, number2, answer;” Are our variables we declared. If you remember in the last tutorial we declared variables and set a value to them. You don’t always have to set a value to them, and you can declare multiple variables in one line, just as long as it is the same variable type.

 

ex. int num1, num2, num3;
ex2. string str1, str2, str3;

 

 

“cout” (pronounced c-out) prints text on the screen. you use the insertion operator (<<) when you want to add a variable, or a string or to end the line with “endl;”.

 

“cin” (pronounced c-in) allows the user to input something. Usually, it is used to assign a variable. In this tutorial, we are using “cin” to assign the variables “number1” and “number2” a value.

 

 

The text between the slashes and asterisks is a block comment. It works just like a comment, the only difference is that you can use multiple lines. the slash and asterisk (/*) is the opening of the comment, and the asterisk and the slash (*/) closes the comment.

 

 

The rest of the code should be easy to understand. In “answer = number1 + number2;” you are setting the variable answer equal to the other two variables you input. then with “cout” you tell the program to print out the answer.

 

You can do more than just addition in c++. Here is a list of operators you can try:

 

+    addition
–    subtraction
*    multiplication
/    division
%    modulo (like division but returns the remainder)

 

 

I will finish this tutorial here. If you feel like it, mess around a little bit with the code. Try some formulas you remembered in school and see if you can get it to make the calculations correctly. This is a good way to learn programming. Happy programming!

Facebooktwittergoogle_plusredditpinterestlinkedinmail

About Justin

Computer programmer and owner of kauergames.com.
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.