0:00
Hi, welcome back guys to the new video on our channel
0:15
In today's video, I'm going to create a very simple calculator for beginners, right
0:22
Using C++ language. So without further delay, let's get started. And remember, if you have any questions, you can ask them in comments, right
0:33
So let's get started. First of all, I will have to import all the required libraries
0:40
So hash include, hash include, iostream, then we have that's it. And now I'll simply type using namespace and std, right
0:54
And then we create our main function as usual. And now I have to get two values from the user at the beginning of the program
1:05
And then after entering two values, a user will have to enter the operator
1:13
That could be addition operator, subtraction, division, or multiplication operator. After that, the program will actually display the result according to the given operator
1:25
It will perform the operation as specified. So let's get two values from the user
1:33
But first, before I get values from the user, I want to prompt the user to enter the first number
1:41
So enter first number, right? And then I'll use cin, and then I'll store the first value in x variable
1:51
So first of all, we have to declare variables before we can assign value in it
1:58
So the first variable is x, and for the second value, I use y variable
2:05
So that's it. Through cin, we're getting the value from the user and storing it in the x variable
2:13
which is going to be the first value. And then we tell the user to enter the second value
2:20
We prompt the user to enter the second value. Enter second number
2:26
And then I use cin again here, and this time I store its value in y variable
2:32
That's it. Now I've simply got the values from the user, both of the values from the user
2:37
Now we simply tell the user to enter any operator. Enter any operator
2:47
And after that, I use cin again, and this time I store its value in op variable
2:54
So let me declare another variable, and it should have a character data type
3:04
So character boop. And that's it. Now I've already got the two values from the user, and the user has also specified the operator
3:15
Now it is simple. The game is simple. Now for this purpose, you can use FL statements, I mean conditional statements, but I want
3:26
to use switch statement here. That would really fit here. So let's get started
3:32
Switch, and then I'll have to pass the operator variable here, and then we make a body for
3:39
the switch statement, and now we're going to define several cases inside the switch statement
3:46
So the first case should be case, which is the first case, if the user has entered, right
3:52
if the user has entered addition operator. So what's going to happen is the program is going to store its value
4:05
The program is going to perform addition, and then display the value, display the result
4:11
to the user. So cout, the sum is, or simply the sum, and then I simply add x plus y, which are two variables
4:26
That's it. And then we use a break statement at the end of each case
4:30
That's the syntax for switch statement, okay? Now we define the second case
4:36
If that's a subtraction, if that's a subtraction operator, then simply prompt the user the
4:48
difference, difference, and that is x minus y. That's it. And now we use break statement at the end of this case as well
4:59
And now we're going to create the third case, which is the multiplication operator, right
5:08
And then I make a sub body for it, and then I simply prompt the user the product
5:19
The product is, or the product, simply the product, is x multiplied by y
5:27
That's it. And a break statement here. And then we have the fourth case, which is for division
5:38
So if the user has entered division symbol, then it's going to perform normal division
5:48
So I'll tell the user the division, division, and then I'll simply divide x by y
5:59
That's it. And then I'll use a break statement here as well. Now remember, at the end of switch statement, we have the option to use a default case
6:09
So if all those cases didn't match, so then default statement will be executed
6:16
So in the default statement, I just want to tell the user that invalid operator
6:21
So if the user enters any operator other than these four operators, then a user will be
6:29
prompted that you have entered an invalid operator. So invalid operator. That's it
6:37
Now we have successfully used switch statement. We have applied switch statement here
6:43
Now we're going to run this program and see if there are any mistakes, then we'll just
6:49
identify them and just correct them
7:06
So now it's compiling here. And as you can see that, yep, there is a problem
7:11
I didn't put this operator here, which is known as the insertion operator. Yep
7:20
So let's compile this code again. Now it's compiling and we've got zero errors here
7:29
Now I'm just going to run this program and you will see the result
7:34
Here is the program. The user, the program asks me for the first number
7:40
So I simply enter suppose 10 here, and then it tells me to enter the second number
7:46
Suppose I entered two here. So now it's asking me for operator, right
7:52
So suppose I simply want to do the division operation here. So I'll simply put a addition, I mean addition symbol here
8:00
I want to perform addition here. So I'll simply use this operator here
8:06
And as you can see on the screen, it tells me that this sum, which is 12, 10 plus two
8:13
equals to 12. So let's rerun it. And now this time I want to put a multiplication operator here
8:23
So suppose eight multiplied by three, and then I want to multiply these two values
8:31
That's it. The product is 24. So it's eight multiplied by three equals to 24, right
8:37
And now we're going to, I want this program to execute the default statement here
8:45
So what's going to happen? I'm going to enter the first number six and then five, and then I enter something else
8:52
in the operator here. Suppose I enter C. So it shows that you have inputted an invalid operator
9:01
So that was just a simple program, a simple calculator in C++, or short program for beginners
9:09
an easy one. And if you still have questions, go down to the comment section and feel free to ask any
9:15
question and I'll be there to answer you. That was all for now
9:20
Thank you for taking your time to watching this video, and I'll catch you guys in the