Need help Basic C++ Calculator, how can I improve?

Master Yoda

Administrator
Administrator
Verified
Jan 2, 2018
1,317
349
95
Canada
codeforum.org
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.
 

Master Yoda

Administrator
Administrator
Verified
Jan 2, 2018
1,317
349
95
Canada
codeforum.org
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++!
 
  • Like
Reactions: Deleted member 205