Design a calculator program that will add, subtract, multiply, or divide two num
ID: 3772944 • Letter: D
Question
Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.
Your program design should contain the following:
The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculator program.
When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displaying the result, prompting the user to add two more different numbers) until the user enters a sentinel value to end the chosen arithmetic operation.
If the user chooses division, do not allow the user to divide by 0. Display an error message to user and ask the user to choose another denominator
Explanation / Answer
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <stdio.h>
using namespace std;
int main ()
{
double num1, num2, solution;
char symbol;
char add = '+';
char subtract = '-';
char multiply = '*';
char divide = '/';
cout << "This calulator takes an equation in the form (number operator number). It adds, subtracts, multiplies and divides any two whole numbers.";
cout << " Example is 5 + 5. You don't need a space between the characters. ";
cout << "Please make sure you enter in a whole number. To exit, please type fin. ";
cout << "please enter in the equation you would like to be solved: ";
cin >> num1 >> symbol >> num2;
while (num1 != 'f' && symbol != 'i' && num2 != 'n')
{
if (symbol == '+')
{
solution = num1 + num2;
cout << "The solution to " << num1 << " + " << num2 << " = " << solution;
cout << " please enter in the equation you would like to be solved: ";
cin >> num1 >> symbol >> num2;
}
while (symbol == '-')
{
solution = num1 - num2;
cout << "The solution to " << num1 << " - " << num2 << " = " << solution;
cout << " please enter in the equation you would like to be solved: ";
cin >> num1 >> symbol >> num2;
}
while (symbol == '*')
{
solution = num1 * num2;
cout << "The solution to " << num1 << " * " << num2 << " = " << solution;
cout << " please enter in the equation you would like to be solved: ";
cin >> num1 >> symbol >> num2;
}
while (symbol == '/')
{
while(num2 == 0)
{
cout << "Error: The denominator cannot be 0!! Please enter a new value for the denominator: ";
cin >> num2;
}
solution = num1 / num2;
cout << "The solution to " << num1 << " / " << num2 << " = " << solution;
cout << " please enter in the equation you would like to be solved: ";
cin >> num1 >> symbol >> num2;
}
break;
}
cout << " Have a nice day!! :)";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.