Answer to Question #128505 in C++ for Ali Rizwan

Question #128505
Design a calculator using the concept of functions. Take user defined values for each operation.
1
Expert's answer
2020-08-05T16:47:02-0400
#include <iostream>


void calculate(float a, char op, float b);
int main()


void calculate(float a, char op, float b)
{
    switch(op) {
        case '+':
            std::cout << "\nAddition of two numbers is " << (a + b);
            break;
        case '-':
            std::cout << "\nSubtraction of two numbers is " << (a - b);
            break;
        case '*':
            std::cout << "\nMultiplication of two numbers is= "<< (a * b);
            break;
        case '/':
        	std::cout << "\nDivision of two numbers is= "<< (a / b);
            break;
        default:
            std::cerr << "\nUnkown operator!";
    }
}

{
    float a, b; // These are useless: sum, sub, mul, divide, mod
    char op; //operands and operators are entered by the user 


    std::cout.exceptions(std::cout.failbit);
    std::cin.exceptions(std::cin.badbit);
    std::cerr.exceptions(std::cerr.failbit);


    std::cout << "Enter any two operands with operator="; 


    if (std::cin >> a >> op >> b)
        calculate(a, op, b);


    return 0;
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS