In: Computer Science
Your Task: Write a calculator Program with following functions (lack of function will result in a grade of zero). Your program must have the following menu and depending on the users choice, your program should be calling the pertaining function to display the result for the user.
1 - Add
2 - Subtract
3 - Multiply
4 - Divide
5 - Raise X to the power Y
6 - Finds if a number is even or odd
0 - Quit
Your calculator must:
1. Display a menu with all the options
2- Ask the user to select an option from the menu
3. Based on the user selection, your program must then ask for the
operand/s and call the pertaining function.
4. Your program will accept int and the result can be displayed in double (utilize type casting).
5.Once your program is done with the user's option, your program must ask if the user would like to try another option utilizing loops)
Input Validation: Your program should only accepts numbers.
Language: C++
Below is the complete C++ solution. If you face any difficulty while understanding the code, Please let me know in the comments.
Code:
#include <iostream>
#include <bits/stdc++.h>
#include <limits>
using namespace std;
// Method to input integer value only
int inputIntegerVal(string inputMsg) {
int input = -1;
bool valid= false;
do {
cout << inputMsg << flush;
cin >> input;
if (cin.good())
valid = true;
else {
cin.clear();
//empty the previous input
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Invalid input. Please enter integer only!" <<
endl;
}
} while (!valid);
return (input);
}
// function to add two numbers
void add(int x, int y) {
cout << "Sum of the numbers is: " << x+y <<
endl;
}
// function to subtract two numbers
void subtract(int x, int y) {
cout << "Difference of the numbers is: " << x-y
<< endl;
}
// function to multiply two numbers
void multiply(int x, int y) {
cout << "Poduct of the numbers is: " << x*y <<
endl;
}
// function to divide two numbers
void divide(int x, int y) {
cout << "Quotient is: " << (double) x/y <<
endl;
}
// function to check number is even or odd
void checkEvenOdd(int num) {
if(num%2 == 0)
cout << num << " is even!" << endl;
else
cout << num << " is odd!" << endl;
}
// function to find power
void findPower(int x, int y) {
cout << "X to the power Y is: " << pow(x,y) <<
endl;
}
// function to quit the program
void quit() {
cout << "Program terminated successfully!" <<
endl;
return 0;
}
int main() {
while (true) {
int choice;
cout << "Choose operation to perform: \n1 - Add\n2 -
Subtract\n3 - Multiply\n4 - Divide";
cout << "\n5 - Raise X to the power Y\n6 - Finds if a number
is even or odd\n0 - Quit" << endl;
cin >> choice;
if(choice == 0)
quit();
else if(choice == 6) {
int num = inputIntegerVal("Enter a number: ");
checkEvenOdd(num);
}
else if(choice >= 1 && choice <=5) {
// take two integers input
int x = inputIntegerVal("Enter first number: ");
int y = inputIntegerVal("Enter second number: ");
// perform operation according to the input choice
switch(choice) {
case 1:
add(x,y);
break;
case 2:
subtract(x,y);
break;
case 3:
multiply(x,y);
break;
case 4:
divide(x,y);
break;
case 5:
findPower(x,y);
break;
}
} else
std::cout << "Please enter a valid choice!" <<
std::endl;
cout << "\n";
}
return 0;
}
Output: