CPPForSchool.com: User Defined Function Set 2 Assignment 1
1.
Write a program that lets the user perform arithmetic operations on
two numbers. Your program must be menu driven, allowing the user to
select the operation (+, -, *, or /) and input the numbers. Furthermore,
your program must consist of following functions:
1. Function showChoice: This function shows the options to the user and explains how to enter data.
2. Function add: This function accepts two number as arguments and returns sum.
3. Function subtract: This function accepts two number as arguments and returns their difference.
4. Function multiply: This function accepts two number as arguments and returns product.
5. Function divide: This function accepts two number as arguments and returns quotient.
------------------------------------------------------------------------------------------------------------------------
Just like when I was in school, it appears the more complex the assignment, the greater chances I have of success. I could never figure out why the simple stuff always got me stuck, while larger problems felt more natural. Nevertheless, I am unable to provide a screenshot of my code nor the sample source. Instead, I have copied and pasted both below. However, I will be able to provide screenshots of the outputs.
**It appears I went with a different direction in regards to order of the menu. The user is blindly prompted for the integers first, then the menu option of arithmetic operators are presented. Also, I did not utilize a do/while loop.**
#include <iostream>
using namespace std;
/* User Defined Function: Arithmetic Operations version 1.0 */
/* Coded by Sherwin Bautista */
/* Assignment by cppforschool.com */
/* http://www.cppforschool.com/assignment/user_2.html */
void showChoices();
float add(float, float);
float subtract(float, float);
float multiply(float, float);
float divide(float, float);
int main()
{
cout << "-------------------------------------" << endl;
cout << "--------ARITHMETIC OPERATIONS--------" << endl;
cout << "----------------BY-------------------" << endl;
cout << "----------SHERWIN BAUTISTA-----------" << endl;
cout << "-------------------------------------" << endl;
float x, y;
int menuSelect;
cout << "Please enter first integer: ";
cin >> x;
cout << "Please enter second integer: ";
cin >> y;
showChoices();
cin >> menuSelect;
switch (menuSelect)
{
case 1:
cout << "The sum of " << x << " and " << y << " is: " << add(x,y);
break;
case 2:
cout << "The difference of " << x << " and " << y << " is: " << subtract(x,y);
break;
case 3:
cout << "The product of " << x << " and " << y << " is: " << multiply(x,y);
break;
case 4:
cout << "The quotient of " << x << " and " << y << " is: " << divide(x,y);
break;
case 5:
cout << "Thank you for playing!" << endl;
break;
default:
cout << "Invalid input, please try again." << endl;
}
return 0;
}
void showChoices()
{
cout << "Please select from the following (1, 2, 3, 4):" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;
cout << "5. Exit" << endl;
}
float add(float a, float b)
{
return a + b;
}
float subtract(float a, float b)
{
return a - b;
}
float multiply(float a, float b)
{
return a * b;
}
float divide(float a, float b)
{
return a / b;
}
1. Function showChoice: This function shows the options to the user and explains how to enter data.
2. Function add: This function accepts two number as arguments and returns sum.
3. Function subtract: This function accepts two number as arguments and returns their difference.
4. Function multiply: This function accepts two number as arguments and returns product.
5. Function divide: This function accepts two number as arguments and returns quotient.
------------------------------------------------------------------------------------------------------------------------
Just like when I was in school, it appears the more complex the assignment, the greater chances I have of success. I could never figure out why the simple stuff always got me stuck, while larger problems felt more natural. Nevertheless, I am unable to provide a screenshot of my code nor the sample source. Instead, I have copied and pasted both below. However, I will be able to provide screenshots of the outputs.
**It appears I went with a different direction in regards to order of the menu. The user is blindly prompted for the integers first, then the menu option of arithmetic operators are presented. Also, I did not utilize a do/while loop.**
#include <iostream>
using namespace std;
/* User Defined Function: Arithmetic Operations version 1.0 */
/* Coded by Sherwin Bautista */
/* Assignment by cppforschool.com */
/* http://www.cppforschool.com/assignment/user_2.html */
void showChoices();
float add(float, float);
float subtract(float, float);
float multiply(float, float);
float divide(float, float);
int main()
{
cout << "-------------------------------------" << endl;
cout << "--------ARITHMETIC OPERATIONS--------" << endl;
cout << "----------------BY-------------------" << endl;
cout << "----------SHERWIN BAUTISTA-----------" << endl;
cout << "-------------------------------------" << endl;
float x, y;
int menuSelect;
cout << "Please enter first integer: ";
cin >> x;
cout << "Please enter second integer: ";
cin >> y;
showChoices();
cin >> menuSelect;
switch (menuSelect)
{
case 1:
cout << "The sum of " << x << " and " << y << " is: " << add(x,y);
break;
case 2:
cout << "The difference of " << x << " and " << y << " is: " << subtract(x,y);
break;
case 3:
cout << "The product of " << x << " and " << y << " is: " << multiply(x,y);
break;
case 4:
cout << "The quotient of " << x << " and " << y << " is: " << divide(x,y);
break;
case 5:
cout << "Thank you for playing!" << endl;
break;
default:
cout << "Invalid input, please try again." << endl;
}
return 0;
}
void showChoices()
{
cout << "Please select from the following (1, 2, 3, 4):" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;
cout << "5. Exit" << endl;
}
float add(float a, float b)
{
return a + b;
}
float subtract(float a, float b)
{
return a - b;
}
float multiply(float a, float b)
{
return a * b;
}
float divide(float a, float b)
{
return a / b;
}
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
Below are the sample source and output.
#include <iostream>
using namespace std;
void showChoices();
float add(float, float);
float subtract(float, float);
float multiply(float, float);
float divide(float, float);
int main()
{
float x, y;
int choice;
do
{
showChoices();
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Sum " << add(x,y) <<endl;
break;
case 2:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Difference " << subtract(x,y) <<endl;
break;
case 3:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Product " << multiply(x,y) <<endl;
break;
case 4:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Quotient " << divide(x,y) <<endl;
break;
case 5:
break;
default:
cout << "Invalid input" << endl;
}
}while (choice != 5);
return 0;
}
void showChoices()
{
cout << "MENU" << endl;
cout << "1: Add " << endl;
cout << "2: Subtract" << endl;
cout << "3: Multiply " << endl;
cout << "4: Divide " << endl;
cout << "5: Exit " << endl;
cout << "Enter your choice :";
}
float add(float a, float b)
{
return a + b;
}
float subtract(float a, float b)
{
return a - b;
}
float multiply(float a, float b)
{
return a * b;
}
float divide(float a, float b)
{
return a / b;
}
Comments
Post a Comment