7. Write a simple C++ program that performs addition, subtraction, multiplication, division, exponentiation, base-10 logarithm, and factorial operations. The program should start with a menu letting the user to choose one of the eight operations the user wants to perform. A sample menu is given below (hint: use loop to calculate more than once, use functions):
Enter:
+ for the addition operation
- for the subtraction operation
* for the multiplication operation
/ for the division operation
^ for the exponentiation operation
l for the base-10 logarithm operation
! for the factorial operation and
q to quit.
-->
After an operation is chosen, the program will ask the user to input operands (hint: there will be only one operand for logarithm and factorial operations). The result should be displaced in the form of a formula, e.g. 2^2=4, log(10) = 1, 4!=24.
Here's what I have so far. I can't enter anything into the program...it's just saying 'press any key to continue'.
#include <iostream>
#include <cmath>
using namespace std;
void addition();
void subtraction();
void multiplication();
void division();
void exponentation();
void logarithm();
void factorial();
int factorial(int num);
char character;
int main()
{
cout << "Enter:" << endl;
cout << "+ for the addition operation" << endl;
cout << "- for the subtraction operation" << endl;
cout << "* for the multiplication operation" << endl;
cout << "/ for the division operation" << endl;
cout << "^ for the exponentiation operation" << endl;
cout << "l for the base-10 logarithm operation" << endl;
cout << "! for the factorial operation and" << endl;
cout << "q to quit." << endl;
switch (character)
{
case '+' : addition();
break;
case '-' : subtraction();
break;
case '*' : multiplication();
break;
case '/' : division();
break;
case '^' : exponentation();
break;
case 'l' : logarithm();
break;
case '!' : factorial();
break;
case 'q' : return 0;
}
}
void addition()
{
int number1;
int number2;
int sum;
cout << "Please input two integer numbers:" << endl;
cin >> number1;
cin >> number2;
sum = number1 + number2;
cout << "The sum of the two numbers is " << sum << "." << endl;
}
void subtraction()
{
int number1;
int number2;
int difference;
cout << "Please input two integer numbers:" << endl;
cin >> number1;
cin >> number2;
difference = number1 - number2;
cout << "The difference of the two numbers is " << difference << "." << endl;
}
void multiplication()
{
int number1;
int number2;
int product;
cout << "Please input two integer numbers:" << endl;
cin >> number1;
cin >> number2;
product = number1 * number2;
cout << "The product of the two numbers is " << product << "." << endl;
}
void division()
{
int number1;
int number2;
int quotient;
cout << "Please input two integer numbers:" << endl;
cin >> number1;
cin >> number2;
quotient = number1 / number2;
cout << "The quotient of the two numbers is " << quotient << "." << endl;
}
void exponentation()
{
int number1;
int number2;
int exponent;
cout << "Please input two integer numbers:" << endl;
cin >> number1;
cin >> number2;
exponent = number1 ^ number2;
cout << "The exponent of " << number1 << "^" << number2 << " is " << exponent << "." << endl;
}
void logarithm()
{
float number;
float logarithm;
cout << "Please input a float number:" << endl;
cin >> number;
logarithm = log10(number);
cout << "The logarithm of log10(" << number << ") is " << logarithm << "." << endl;
}
void factorial()
{
int num1, factor;
cout << "Please enter a number to do the Factorial." << endl;
cin >> num1;
factor = factorial(num1);
cout << num1 << " ! =" << factor << endl;
}
int factorial(int num)
{
int i;
int result = 1;
for ( i = 1; i <= num; ++i)
{
result = result *=i;
}
return result;
}