Home  • Programming • C++

Make you own basic calculator in C++ beginner level

The following program will function as a basic calculator; it will ask the user to input what type of arithmetic operation he would like, and then ask for the numbers on which the operation should be performed. The calculator will then give the output of the operation.
#include <iostream>
using namespace std;

int multiply(int x, int y) { return x*y; }
int divide(int x, int y) {return x/y;}
int add(int x, int y) { return x+y; }
int subtract(int x, int y) {return x-y;}

int main(){
char op='c';
int x, y;

 while(op!='e'){
    cout<<"What operation would you like to perform: add(+), subtract(-), divide(/), multiply(*), [e]xit?"; 
    cin>>op; 
    switch(op) {
     case '+': 
        cin>>x;
        cin>>y;
        cout<<x<<"+"<<y<<"="<<add(x, y)<<endl; 
     break; 
     case '-':
        cin>>x;
        cin>>y;
        cout<<x<<"-"<<y<<"="<<subtract(x, y)<<endl; 
     break;
     case '/': 
        cin>>x; 
        cin>>y;
        cout<<x<<"/"<<y<<"="<<divide(x, y)<<endl; 
     break;
     case '*':
        cin>>x; 
        cin>>y; 
        cout<<x<<"*"<<y<<"="<<multiply(x, y)<<endl;
     break;
     case 'e':
        return 0; 
        default:
        cout<<"Sorry, try again"<<endl; 
    }//end swich-case
 }//end while
 
return 0;
}//end main

Comments 0


Share

Copyright © 2024. Powered by Intellect Software Ltd