1. The Math Program (this is the title you want your void function to display) 1
ID: 3811253 • Letter: 1
Question
1.
The Math Program (this is the title you want your void function to display)
1. Add
2. Multiply
3. Exit
Here is my code :
using namespace std;
// Function declarations
double addition(double num1, double num2);
double multiplication(double num1, double num2);
int main()
{
// Declaring variables
int choice;
double sum, multiply, product;
double num1, num2;
// This loop continues to run until the user enters choice 3
while (true)
{
// Displaying the menu
cout << " Menu" << endl;
cout << "----" << endl;
cout << "1. Add" << endl;
cout << "2. Multiply" << endl;
cout << "3. Exit" << endl;
cout << "Enter choice :";
cin >> choice;
switch (choice)
{
case 1:
{
cout << "Enter first number :";
cin >> num1;
cout << "Enter second number :";
cin >> num2;
sum = addition(num1, num2);
// Displaying the sum of two numbers
cout << "The sum of " << num1 << " and " << num2 << " is " << sum << endl;
continue;
}
case 2:
{
// Getting the inputs numbers entered by the user
cout << "Enter first number :";
cin >> num1;
cout << "Enter second number :";
cin >> num2;
product = multiplication(num1, num2);
// Displaying the product of two numbers
cout << "The Product of " << num1 << " and " << num2 << " is " << product << endl;
continue;
}
case 3:
{
cout << ":: Program Exit ::" << endl;
break;
}
default:
{
cout << "Invalid input.Must be either 1 or 2 or 3" << endl;
continue;
}
}
break;
}
return 0;
}
// Function implementation which add two numbers
double addition(double num1, double num2)
{
return num1 + num2;
}
// Function implementation which multiplies two numbers
double multiplication(double num1, double num2)
{
return num1 * num2;
}
Explanation / Answer
Hi
I have modified the code as per your requirement and highlighted the code changes below
#include <iostream>
using namespace std;
// Function declarations
double addition(double num1, double num2);
double multiplication(double num1, double num2);
void displayMenu();
int main()
{
// Declaring variables
int choice;
double sum, multiply, product;
double num1, num2;
// This loop continues to run until the user enters choice 3
while (true)
{
// Displaying the menu
displayMenu();
cin >> choice;
switch (choice)
{
case 1:
{
cout << "Enter first number :";
cin >> num1;
cout << "Enter second number :";
cin >> num2;
sum = addition(num1, num2);
// Displaying the sum of two numbers
cout << "The sum of " << num1 << " and " << num2 << " is " << sum << endl;
continue;
}
case 2:
{
// Getting the inputs numbers entered by the user
cout << "Enter first number :";
cin >> num1;
cout << "Enter second number :";
cin >> num2;
product = multiplication(num1, num2);
// Displaying the product of two numbers
cout << "The Product of " << num1 << " and " << num2 << " is " << product << endl;
continue;
}
case 3:
{
cout << ":: Program Exit ::" << endl;
break;
}
default:
{
cout << "Invalid input.Must be either 1 or 2 or 3" << endl;
continue;
}
}
break;
}
return 0;
}
// Function implementation which add two numbers
double addition(double num1, double num2)
{
return num1 + num2;
}
// Function implementation which multiplies two numbers
double multiplication(double num1, double num2)
{
return num1 * num2;
}
void displayMenu() {
cout << " The Math Program" << endl;
cout << "----" << endl;
cout << "1. Add" << endl;
cout << "2. Multiply" << endl;
cout << "3. Exit" << endl;
cout << "Enter choice :";
}
Output:
The Math Program
----
1. Add
2. Multiply
3. Exit
Enter choice :1
Enter first number :2
Enter second number :3
The sum of 2 and 3 is 5
The Math Program
----
1. Add
2. Multiply
3. Exit
Enter choice :2
Enter first number :2
Enter second number :3
The Product of 2 and 3 is 6
The Math Program
----
1. Add
2. Multiply
3. Exit
Enter choice :3
:: Program Exit ::
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.