KingBowser
Rookie
This is a python solution (first time using python), so adapted a template to fit the problem
A C++ solution
Code:
# calculator program
# NO CODE IS REALLY RUN HERE, IT IS ONLY TELLING US WHAT WE WILL DO LATER
# Here we will define our functions
# this prints the main menu, and prompts for a choice
def menu():
#print what options you have
return input ("1. Addition\n2. Multiplication\n3. Division\n4. Subtraction\n5. Exit\n")
# this adds two numbers given
def add(a,b):
print "The output is: ", a + b
# this subtracts two numbers given
def sub(a,b):
print "The output is: ", b - a
# this multiplies two numbers given
def mul(a,b):
print "The output is: ", a * b
# this divides two numbers given
def div(a,b):
print "The output is: ", a / b
# NOW THE PROGRAM REALLY STARTS, AS CODE IS RUN
loop = 1
choice = 0
another = ""
while loop == 1:
choice = menu()
if choice == 1:
print "You have chosen Addition"
add(input("Enter number A: "),input("Enter number B: "))
elif choice == 2:
print "You have chosen Multiplication"
mul(input("Enter number A: "),input("Enter number B: "))
elif choice == 3:
print "You have chosen Division"
div(input("Enter number A: "),input("Enter number B: "))
elif choice == 4:
print "You have chosen Subtraction"
sub(input("Enter number A: "),input("Enter number B: "))
elif choice == 5:
loop = 0
if choice != 5:
another = raw_input("Would you like to perform another operation?Y/N ")
if another == "Y":
loop = 1
elif another == "N":
loop = 0
# NOW THE PROGRAM REALLY FINISHES
A C++ solution
Code:
#include<iostream>
#include<string>
void calculate(const int &option);
void getValidInt(int &num);
void runAgain(int &option);
int main()
{
int option = 0;
while (option != 5)
{
std::cout << "Welcome to Calculator, which operation would you like to perform?" << std::endl;
std::cout << "1. Addition\n2. Multiplication\n3. Division\n4. Subtraction\n5. Exit\n";
std::cin >> option;
if (std::cin.fail() || option < 1 || option > 5)
{
std::cin.clear();
std::cin.ignore(INT_MAX, '\n');
std::cout << "Please enter valid option (1 - 5).\n";
}
else
{
if (option != 5)
{
switch (option){
case 1: std::cout << "You have chosen Addition.\n";
break;
case 2: std::cout << "You have chosen Multiplication.\n";
break;
case 3: std::cout << "You have chosen Division.\n";
break;
case 4: std::cout << "You have chosen Subtraction.\n";
break;
}
calculate(option);
runAgain(option);
}
}
}
}
void runAgain(int &option)
{
char runOption;
bool valid = false;
while (!valid)
{
std::cout << "Would you like to perform another operation? (Y for Yes, N for No) \n";
std::cin >> runOption;
if (runOption == 'Y' || runOption == 'y')
{
valid = true;
}
else if (runOption == 'N' || runOption == 'n')
{
option = 5;
valid = true;
}
else
{
std::cout << "Invalid entry - Please enter valid option\n";
valid = false;
}
}
}
void getValidInt(int &num)
{
std::cin >> num;
while (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(INT_MAX, '\n');
std::cout << "Please enter a valid number:\n";
std::cin >> num;
}
}
void calculate(const int &option)
{
int num1, num2;
std::cout << "Enter number A:\n";
getValidInt(num1);
std::cout << "Enter number B:\n";
getValidInt(num2);
std::cout << "The output is:\n";
switch (option){
case 1:
std::cout << std::to_string(num1 + num2);
break;
case 2:
std::cout << std::to_string(num1 * num2);
break;
case 3:
std::cout << std::to_string(num1 / num2);
break;
case 4:
std::cout << std::to_string(num1 - num2);
break;
}
std::cout << std::endl;
}