Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ How I built my basic calculator in C++

Malcolm

Administrator
Administrator
Staff Team
Code Plus
Company Plus
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:

Must HaveLater Have
  • Take two numbers and turn it into a result (Add).
  • Make sure that the user can understand what the calculator is asking for and what it is displayed (So instructions).
  • Read character input and determine what mathematical operator to use (+, -, / and *).
  • A visable UI.

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;
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: 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;
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! 🙂
 
Last edited:

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom