Hello all!
From learning C++ to Full-Stack Web Development and job prep I have been all over the place these last couple of days. I recently learned the if/else if/else statements with C++ and have used it to improve the basic calculator that I posted back March - (you can view that here).
With this update to the code, after you input
I had to add another int labelled as int maths, this will contain the input from the user on what operator they wish to use. From there I add the following lines (Basic cout and cin, prompting the user to input their input) the result will then be stored in the int maths.
Now before we had a simple
So what I did was if maths equals 1 then display a statement that adds the two int
In code:
So if(maths equal = 1) then display firstNum + secNum and return to main. I added the return to main(); (usually return 0 which will kill the program) because instead of exiting the program then restart to calculate another calculation it will now go back to the start. However we aren't done, what if you have to subtract or divide?
At the end of the if statement after the closing curly brace
Now that solves that, however I have multiplication, divide and modulo left. So I continue to the same until I have them all added.
However, now you can't finish an if statement with an else if (I don't so, I'll test it). But also, what if someone inputs 6 or anyother number higher? They probably wouldn't get the answer that they want. So lets finish off the if/else if statement with else. SO if none of these are true display an unknown operator message. Like this:
So all together we will have:
From learning C++ to Full-Stack Web Development and job prep I have been all over the place these last couple of days. I recently learned the if/else if/else statements with C++ and have used it to improve the basic calculator that I posted back March - (you can view that here).
With this update to the code, after you input
firstNum
& secNum
you will then be asked what maths operator you'd like to use. Depending on what operator you select, it will calculate the values from firstNum
and secNum
and display the result.I had to add another int labelled as int maths, this will contain the input from the user on what operator they wish to use. From there I add the following lines (Basic cout and cin, prompting the user to input their input) the result will then be stored in the int maths.
C++:
// Asking for operator
cout << "What operator? (1: Addition 2: Subtraction 3: Multiplication 4: Divide 5: modulo)\n";
cin >> maths;
Now before we had a simple
firstNum + secNum
and that was how it displayed the answer. However, this isn't going to be too useful when you'd have to go back into the code and edit the math operator when you needed to add or subtract etc. My solution to this came shortly after I learned about if statements, so remember an if statement works when if the condition is true then do this e.g.
Code:
if (condition) {
true then display this.
}
So what I did was if maths equals 1 then display a statement that adds the two int
firstNum & secNum
together.Note: Keep in mind of this numbers 1 Addition, 2 Subtraction, 3 Multiplication, 4 Divide, 5 Modulo.
In code:
C++:
if (maths==1) {
cout << firstNum + secNum << endl;
return main();
}
At the end of the if statement after the closing curly brace
}
I added the word else if. This says "Okay, the first if statement wasn't true, let's go to the next (If available)". The else if statement follows after the if statement. It is very similar all that's changed is that if now has else in front of it. The code is as shown below.
C++:
if (maths==1) {
cout << firstNum + secNum << endl;
return main();
} else if (maths==2) {
cout << firstNum - secNum << endl;
return main();
}
C++:
if (maths==1) {
cout << firstNum + secNum << endl;
return main();
} else if (maths==2) {
cout << firstNum + secNum << endl;
return main();
} else if (maths==3) {
cout << firstNum * secNum << endl;
return main();
} else if (maths==4) {
cout << firstNum / secNum << endl;
return main();
} else if (maths==5) {
cout << firstNum % secNum << endl;
return main();
}
C++:
} else {
cout << "Unknown Operator\n"
}
So all together we will have:
C++:
#include <iostream>
using namespace std;
int main(){
int firstNum {0};
int secNum {0};
int maths {0};
// Asking for two numbers
cout << "Enter your first number\n";
cin >> firstNum;
cout << "Enter your second number\n";
cin >> secNum;
// Asking for operator
cout << "What operator? (1: Addition 2: Subtraction 3: Multiplication 4: Divide 5: modulo)\n";
cin >> maths;
if(maths==1) {
cout << firstNum + secNum << endl;
return main();
} else if (maths==2){
cout << firstNum - secNum << endl;
return main();
} else if (maths==3){
cout << firstNum * secNum << endl;
return main();
} else if (maths==4){
cout << firstNum / secNum << endl;
return main();
} else if (maths==5){
cout << firstNum % secNum << endl;
return main();
} else {
cout << "Unknown Operator \n";
}
return main();
}