Question

In: Computer Science

Your Task: Write a calculator Program with following functions (lack of function will result in a...

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++

Solutions

Expert Solution

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:


Related Solutions

Your Task: Write a calculator Program with following functions (lack of function will result in a...
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...
Take the following program and include overload functions into it. Display result of function. C++ Program:...
Take the following program and include overload functions into it. Display result of function. C++ Program: #include <iostream> using namespace std; // this is the additional function string read() {     string input;     cout << "Enter input: ";     cin >> input;     return input; } int main() {     // call function here     string output = read();     cout << "You entered: " << output; }
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
Write a program that contains 2 functions. Program will call a function named calc_commission that prompt...
Write a program that contains 2 functions. Program will call a function named calc_commission that prompt the user to enter the sales amount and computes and prints with a description the commission paid to salesperson as follows: 10% for sales amount less than $2,000.00, 15% for sales amount less than $10,000.00 and 20% for sales amount less than $20,000.00, then function calc_commission calls another function name assign_base_salary() to ask the user to enter each of 5 salesperson’s base salary ,...
python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow...
python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow of the program (calls the other modules) •userInput() Asks the user to enter two numbers •add() Accepts two numbers, returns the sum •subtract() Accepts two numbers, returns the difference of the first number minus the second number •multiply() Accepts two numbers, returns the product •divide() Accepts two numbers, returns the quotient of the first number divided by the second number •modulo() Accepts two numbers,...
Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
Write a program fragment (not a complete program) which will perform the following task: The int...
Write a program fragment (not a complete program) which will perform the following task: The int variable m currently contains the number of minutes a basketball player played in their last game. Use an IF statement(s) to print out an appropriate message based on the following: If m is from 35 to 48, print the message "very tired" If m is from 10 to 34, print the message "little tired" If m is from 1 to 9, print the message...
Your task is to write a program in C or C++ that calculates the total amount...
Your task is to write a program in C or C++ that calculates the total amount of money a person has made over the last year. Your program will prompt the user for dollar amounts showing how much the user has made per job. Some users will have had more than one job, make sure your program allows for this. The program will print out the total made (all jobs) and will print out the federal and state taxes based...
write a program to make scientific calculator in java
Problem StatementWrite a program that uses the java Math library and implement the functionality of a scientific calculator. Your program should have the following components:1. A main menu listing all the functionality of the calculator.2. Your program should use the switch structure to switch between various options of the calculator. Your Program should also have the provision to handle invalidoption selection by the user.3. Your calculator SHOULD HAVE the following functionalities. You can add any other additional features. PLEASE MAKE...
Write the following task in C++1) Write the definition of a function numOccurrences thatsearches...
Write the following task in C++1) Write the definition of a function numOccurrences that searches for a character in a character array and returns the number of times it occurs in the array. The function has three formal parameters: a char array array, an int variable arraySize representing the size of the array, and a character variable letter representing the character to be searched for in the array.2) Assume the array numbers is an int array of size 10 and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT