Welcome!

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

SignUp Now!
Resource icon

My basic calculator code for C++

Hello Coders!

Here's my calculator code if you wish to experiment or build onto it! 🙂
C++:
//
// Created by malcolm on 2020-07-31.
//
// Advance Calculator

#include <iostream>
using namespace std;

int main(){
    int firstNum {0};
    int secNum {0};
    int maths {0};

    // Asking for two numbers
    cout << "Enter your first number\n";
    cin >> firstNum;
    cout << "Enter your second number\n";
    cin >> secNum;

    // Asking for operator
    cout << "What operator? (1: Addition 2: Subtraction 3: Multiplication 4: Divide 5: modulo)\n";
    cin >> maths;

    if(maths==1) {
        cout << firstNum + secNum << endl;
        return main();
    } else if (maths==2){
        cout << firstNum - secNum << endl;
        return main();
    } else if (maths==3){
        cout << firstNum * secNum << endl;
        return main();
    } else if (maths==4){
        cout << firstNum / secNum << endl;
        return main();
    } else if (maths==5){
        cout << firstNum % secNum << endl;
        return main();
    } else {
        cout << "Unknown Operator \n";
    }
    return main();
}

[CODE lang="cpp" title="Calculator - OLD"]#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]
Author
Malcolm
Views
711
First release
Last update

Ratings

0.00 star(s) 0 ratings

More resources from Malcolm

Latest updates

  1. Use multiple math operators without exiting console

    // // Created by malcolm on 2020-07-31. // // Advance Calculator #include <iostream> using...
Back
Top Bottom