Hi, welcome back guys to the new video on our channel
In today's video, I'm going to create a very simple calculator for beginners, right
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
So let's get started. First of all, I will have to import all the required libraries
So hash include, hash include, iostream, then we have that's it. And now I'll simply type using namespace and std, right
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
And then after entering two values, a user will have to enter the operator
That could be addition operator, subtraction, division, or multiplication operator. After that, the program will actually display the result according to the given operator
It will perform the operation as specified. So let's get two values from the user
But first, before I get values from the user, I want to prompt the user to enter the first number
So enter first number, right? And then I'll use cin, and then I'll store the first value in x variable
So first of all, we have to declare variables before we can assign value in it
So the first variable is x, and for the second value, I use y variable
So that's it. Through cin, we're getting the value from the user and storing it in the x variable
which is going to be the first value. And then we tell the user to enter the second value
We prompt the user to enter the second value. Enter second number
And then I use cin again here, and this time I store its value in y variable
That's it. Now I've simply got the values from the user, both of the values from the user
Now we simply tell the user to enter any operator. Enter any operator
And after that, I use cin again, and this time I store its value in op variable
So let me declare another variable, and it should have a character data type
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
Now it is simple. The game is simple. Now for this purpose, you can use FL statements, I mean conditional statements, but I want
to use switch statement here. That would really fit here. So let's get started
Switch, and then I'll have to pass the operator variable here, and then we make a body for
the switch statement, and now we're going to define several cases inside the switch statement
So the first case should be case, which is the first case, if the user has entered, right
if the user has entered addition operator. So what's going to happen is the program is going to store its value
The program is going to perform addition, and then display the value, display the result
to the user. So cout, the sum is, or simply the sum, and then I simply add x plus y, which are two variables
That's it. And then we use a break statement at the end of each case
That's the syntax for switch statement, okay? Now we define the second case
If that's a subtraction, if that's a subtraction operator, then simply prompt the user the
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
And now we're going to create the third case, which is the multiplication operator, right
And then I make a sub body for it, and then I simply prompt the user the product
The product is, or the product, simply the product, is x multiplied by y
That's it. And a break statement here. And then we have the fourth case, which is for division
So if the user has entered division symbol, then it's going to perform normal division
So I'll tell the user the division, division, and then I'll simply divide x by y
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
So if all those cases didn't match, so then default statement will be executed
So in the default statement, I just want to tell the user that invalid operator
So if the user enters any operator other than these four operators, then a user will be
prompted that you have entered an invalid operator. So invalid operator. That's it
Now we have successfully used switch statement. We have applied switch statement here
Now we're going to run this program and see if there are any mistakes, then we'll just
identify them and just correct them
So now it's compiling here. And as you can see that, yep, there is a problem
I didn't put this operator here, which is known as the insertion operator. Yep
So let's compile this code again. Now it's compiling and we've got zero errors here
Now I'm just going to run this program and you will see the result
Here is the program. The user, the program asks me for the first number
So I simply enter suppose 10 here, and then it tells me to enter the second number
Suppose I entered two here. So now it's asking me for operator, right
So suppose I simply want to do the division operation here. So I'll simply put a addition, I mean addition symbol here
I want to perform addition here. So I'll simply use this operator here
And as you can see on the screen, it tells me that this sum, which is 12, 10 plus two
equals to 12. So let's rerun it. And now this time I want to put a multiplication operator here
So suppose eight multiplied by three, and then I want to multiply these two values
That's it. The product is 24. So it's eight multiplied by three equals to 24, right
And now we're going to, I want this program to execute the default statement here
So what's going to happen? I'm going to enter the first number six and then five, and then I enter something else
in the operator here. Suppose I enter C. So it shows that you have inputted an invalid operator
So that was just a simple program, a simple calculator in C++, or short program for beginners
an easy one. And if you still have questions, go down to the comment section and feel free to ask any
question and I'll be there to answer you. That was all for now
Thank you for taking your time to watching this video, and I'll catch you guys in the
next one