• 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.
    • 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
Verified
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 . . .
 
D

Deleted member 205

Guest
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.
 

Malcolm

Administrator
Administrator
Verified
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++!
 
Top Bottom