Hello Coders!
Today, I want to teach you how I created a basic C++ calculator. I built this calculator to help practice my newly learnt knowledge with C++.
In my online Udemy course, I was taught how to display text to the console, collect user input and store them into variables. With this I wanted to practice what I learned, so I decided what could I make to help me understand what I just learned. I thought of a few ideas such as an age checker and a calculator. You're probably thinking "...oh you did the age checker first" nope, I went on to do the calculator simply because at this point haven't yet learned how to use functions in C++ and would require this to make an age checker but I learned how to create functions shortly after.
So now that I had a calculator in mind to build to test my knowledge I had to figure out what I had to do in order for me to create this calculator. Before I thought about how to build it, I needed to come up with a plan. I did not want to start building this calculator and getting stuck because I have created an unachievable goal that I had set for myself due to wanting to add advanced features. Then that's when I came up with a chart, a two-column table labelled: my must-haves and later haves. Similar to the one below:
So now that I had a guide of what I should be focused on, I went onto the next step... I thought, Okay, I need to have a place to store the value 1 and 2 then the final result. I had to create three variables: firstNum, secondNum and finalNum.
Within my
Now, that I have my variables I need to be able to ask the user to input their first number. Just what I mentioned within my Must have column, I need to make sure that the user understands how to use my calculator. In order for me to do this, I need to display text to the console, I do this by writing the following line:
I have the first number, I need to get the users second number. I repeat the above instructions:
All together this will look like:
Let's add the two numbers that the user had inputted. I did this by saying I want to have
This will then display the result to the console.
Now all together!
[CODE lang="cpp" title="All together"]
#include <iostream>
int main (){
int firstNum;
int secondNum;
int finalNum;
// Asking for first number
std::cout << "Input first number" << std::endl;
std::cin >> firstNum;
// Asking for second number
std::cout << "Input second number" << std::endl;
std::cin >> secondNum;
// Add two values together into finalNum
finalNum = firstNum+secondNum;
//Display result to user
std::cout << "Your result" << std::endl;
std::cout << finalNum << std::endl;
return 0;
}[/CODE]
I hope that I inspired you guys to learn, practice, build and share!
Today, I want to teach you how I created a basic C++ calculator. I built this calculator to help practice my newly learnt knowledge with C++.
In my online Udemy course, I was taught how to display text to the console, collect user input and store them into variables. With this I wanted to practice what I learned, so I decided what could I make to help me understand what I just learned. I thought of a few ideas such as an age checker and a calculator. You're probably thinking "...oh you did the age checker first" nope, I went on to do the calculator simply because at this point haven't yet learned how to use functions in C++ and would require this to make an age checker but I learned how to create functions shortly after.
So now that I had a calculator in mind to build to test my knowledge I had to figure out what I had to do in order for me to create this calculator. Before I thought about how to build it, I needed to come up with a plan. I did not want to start building this calculator and getting stuck because I have created an unachievable goal that I had set for myself due to wanting to add advanced features. Then that's when I came up with a chart, a two-column table labelled: my must-haves and later haves. Similar to the one below:
Must Have | Later Have |
---|---|
|
|
So now that I had a guide of what I should be focused on, I went onto the next step... I thought, Okay, I need to have a place to store the value 1 and 2 then the final result. I had to create three variables: firstNum, secondNum and finalNum.
Within my
int main() {}
I added the following integers without value(s) as they will be filled later.
C++:
int firstNum;
int secondNum;
int finalNum;
std::cout << "Input first number" << std::endl;
. Now, I need for the console to listen to user input and store it into the variable firstNum. I add the following line: std::cin >> firstNum;
. This will now store whatever is inputted by the user into the firstNum variable.I have the first number, I need to get the users second number. I repeat the above instructions:
std::cout << "Input second number" << std::endl;
and std::cin >> secondNum;
.All together this will look like:
C++:
int firstNum;
int secondNum;
int finalNum;
// Asking for first number
std::cout << "Input first number" << std::endl;
std::cin >> firstNum;
// Asking for second number
std::cout << "Input second number" << std::endl;
std::cin >> secondNum;
Let's add the two numbers that the user had inputted. I did this by saying I want to have
finalNum
equal to firstNum+secondNum
. This will make the finalNum be the result of adding both firstNum and secondNum together. Now, that we have the final number let's display it to the user.
C++:
std::cout << "Your result:" << std::endl;
std::cout << finalNum << std::endl;
Now all together!
[CODE lang="cpp" title="All together"]
#include <iostream>
int main (){
int firstNum;
int secondNum;
int finalNum;
// Asking for first number
std::cout << "Input first number" << std::endl;
std::cin >> firstNum;
// Asking for second number
std::cout << "Input second number" << std::endl;
std::cin >> secondNum;
// Add two values together into finalNum
finalNum = firstNum+secondNum;
//Display result to user
std::cout << "Your result" << std::endl;
std::cout << finalNum << std::endl;
return 0;
}[/CODE]
I hope that I inspired you guys to learn, practice, build and share!
Last edited: