Making of a Basic Calculator in C++ Part (1/8)

2
192

Hello guys, I hope you are doing all right. So from this post to the next 8 posts, we will be making a basic calculator in C++. I know I have told you just nothing till now but I can assure you that you will be learning a lot through this series. So without any delays, let’s start.

Hey Did you check the Introduction to C++

One of the most important things that comes into mind when making a calculator is asking the user for input of the numbers on which we are going to perform the operation. so let’s see how can we do that.

Suppose that we are going to ask the user to input a number

So first we have to define some sort of space or memory in which whatever user input will we stored. In c++ we store data inside a variable. As the name suggests variable is something we can change as per requirement. So that’s true in c++ too but the only difference is that a variable can store only a specific datatype only. So a variable can store either integer or decimal values or text values or character values. These are only a few examples.. later on, we will see that we can create our own variable types.

// defining a variable

variable_type variable_name = variable_value;

STANDARD TYPE OF VARIABLES IN C++

1. int:- you can understand that int signifies integer. so a variable of int type will store integer from -2^32 to 2^32 -1.

// for example

int x = 892;

int y = 289;

2. char:- It is used to define characters. By characters, I mean single characters like ‘s’, ‘@’ etc. you can also define characters through their ASCII values too (find what ASCII value means on your own).

/////// examples

char x = ‘c’; // note that character is enclosed between single quotation marks

char x = 65; // 65 is the ASCII value of x.

3. string:- simply putting into words, using string you can define text values. more theoretically, the string is array/collection of characters.

// example

string username = “ashasa892;

string define_yourself = “we are techistaan”;

4. float and double:- using float and double you can define decimal values. (find the difference between float and double on your own).

// example

float x = 89.2;

double x = 8.920;

I have told you 5 types of data types. Other is similar to them except for the fact that they have different ranges. so you have to find about short int, long int, long long int, long double and some other datatypes on your own.

so far we have learned how to store the data inputted by the user. Now input can be taken easily through “cin” function like follows:-

int x;

cout << “Please enter a number: ” << endl;

cin >> x; // note that >> is used for cin.

So that’s it for today. just play with this maths and input/output (i/o) more than last time and enjoy. Don’t forget to complete the task I gave you in the article.

LEARN EXTRA:WHY RANGE OF int IS ONLY TILL 2^32-1 AND NOT TILL 2^32?


Check the second part of Making a Basic Calculator


Stay tuned with Techistaan to get unbiased smartphone & gadgets reviews and latest tech updates.

Follow Us

Facebook

Instagram

Tumblr

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here