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++ Basic C++ Calculator, how can I improve?

Malcolm

Administrator
Administrator
Staff Team
Code Plus
Company Plus
Hello fellow coders!

I'm currently learning C++ off Udemy and I decided to try something on my own and build a simple calculator. I shared the code down below, but what I want to know is what can I do to improve this code?

First, I declared three integers firstNum, secondNum and finalNum. Now, I need to gain input from the user I do this by using CIN (Character In) and have the user input placed into one of the integers I created at the beginning (firstNum & secondNum). Then the compiler adds the two integers together and places the result into finalNum.

C++:
#include <iostream>

int main() {
    int firstNum;
    int secondNum;
    int finalNum;
    
    std::cout << "Input first number" << std::endl;
    std::cin >> firstNum;
    std::cout << "Input second number" << std::endl;
    std::cin >> secondNum;
    
    finalNum = firstNum+secondNum;
    std::cout << "Calculating..." << std::endl;
    std::cout << finalNum << std::endl;
    
    return 0;
}

Input first number
5
Input second number
5
Calculating...
10
Press any key to continue . . .
 
I don't see any issues with the Coding-Style so you're good with that.

One suggestion that I can make is instead of giving it the ability to just add numbers, maybe make it so that it can also do subtraction, multiplication and division. If you don't know Functions yet then get around to doing that and then come back to this.

Good luck with learning C++. It's not an easy Language but I'm sure you'll get through it. And believe me, it will be beneficial for a career in Software, Systems, Networking and Video Games.
 
One suggestion that I can make is instead of giving it the ability to just add numbers, maybe make it so that it can also do subtraction, multiplication and division. If you don't know Functions yet then get around to doing that and then come back to this.
That’s the plan! I’m thinking of having it where at the beginning it will ask you the type of calculation. Hopefully I can do this by using getline and if content matches a keyword then use this function.

But haven’t learned functions just yet. So hopefully I’ll get to that soon!
Good luck with learning C++. It's not an easy Language but I'm sure you'll get through it. And believe me, it will be beneficial for a career in Software, Systems, Networking and Video Games.
It definitely isn’t easy but it’s fun when you figure something out. Looking forward to learning C++!
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom