//==========================================================
/*Description:
This program is to showcase my understanding of
functions that I learned from our Lecture 7b. The user
is prompted to enter two integer values, where they
are then passed to a calculation function to calculate
the sum, difference, product, quotient, and remainder
of the two numbers entered. After all the values are
calculated, they are showcased in a display function
to the user in the output stream.*/
//==========================================================
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void getData(int& num1, int& num2);
void doTheMath(int num1, int num2, int& sum, int& diff, int& prod, int& quot, int& rem);
void displayResults(int num1, int num2, int sum, int diff, int prod, int quot, int rem);
//====================== main ===========================
//
//=======================================================
int main()
{
int num1;
int num2;
int sum;
int diff;
int prod;
int quot;
int rem;
//Gets two integers from user
getData(num1, num2);
//Does the calculation from integers received
doTheMath(num1, num2, sum, diff, prod, quot, rem);
//Displays calculated results from two integers
displayResults(num1, num2, sum, diff, prod, quot, rem);
system("pause");
return 0;
}
/*===================== getData ==========================
This function gets the information from the user of the
two integers they wish to input. It assigns the user's
numbers to num1 and num2.
Input:
num1 - First integer assigned by user
num2 - Second integer assigned by user
Output:
The values being assigned to be used in the doTheMath
function.*/
//========================================================
void getData(int& num1, int& num2)
{
cout << "Please enter two integer values:\n";
cin >> num1;
cin >> num2;
}
/*==================== doTheMath =========================
This function calculates the user's two integers inputted
into the previous function and assigns the calculated
answers to variables named by the calculation performed.
It first checks to see if num2 is 0, because this system
can't divide by zero without crashing.
Input:
sum - adds the two integers
diff - subtracts the two integers
prod - multiplies the two integers
quot - divides the two integers
rem - gets the remainder of the two integers
Output:
Variables are now assigned new values to be displayed
inside of the displayResults function.*/
//========================================================
void doTheMath(int num1, int num2, int& sum, int& diff, int& prod, int& quot, int& rem)
{
if (num2 == 0)
{
sum = (num1 + num2);
diff = (num1 - num2);
prod = (num1 * num2);
}
else
{
sum = (num1 + num2);
diff = (num1 - num2);
prod = (num1 * num2);
quot = (num1 / num2);
rem = (num1 % num2);
}
}
/*================= displayResults ======================
This function takes the calculations from the doTheMath
function and displays them to the user in a standard
output stream. It first checks to see if num2 is 0,
because this system can't divide by zero without
crashing.
Input:
Calculations from the doTheMath function, as well as
num1 and num2.
(sum, diff, prod, quot, rem).
Output:
Displays the calculations from the doTheMath function
to the user in a standard output stream.*/
//========================================================
void displayResults(int num1, int num2, int sum, int diff, int prod, int quot, int rem)
{
if (num2 == 0)
{
cout << "Here are the results:\n\n";
cout << "The sum of " << num1 << " and " << num2
<< " is " << sum << ".\n";
cout << "The difference, (" << num1 << " minus "
<< num2 << ") is " << diff << ".\n";
cout << "The product of " << num1 << " and "
<< num2 << " is " << prod << ".\n";
cout << "Cannot divide by zero.\n\n";
}
else
{
cout << "Here are the results:\n\n";
cout << "The sum of " << num1 << " and " << num2
<< " is " << sum << ".\n";
cout << "The difference, (" << num1 << " minus "
<< num2 << ") is " << diff << ".\n";
cout << "The product of " << num1 << " and "
<< num2 << " is " << prod << ".\n";
cout << num1 << " divided by " << num2 << " is "
<< quot << " with a remainder of " << rem
<< ".\n\n";
}
}
//==========================================================
/*OUTPUT (When num2 != 0):
Please enter two integer values:
12
3
Here are the results:
The sum of 12 and 3 is 15.
The difference, (12 minus 3) is 9.
The product of 12 and 3 is 36.
12 divided by 3 is 4 with a remainder of 0.
Press any key to continue . . .*/
//==========================================================
//==========================================================
/*OUTPUT (When num2 == 0):
Please enter two integer values:
12
0
Here are the results:
The sum of 12 and 0 is 12.
The difference, (12 minus 0) is 12.
The product of 12 and 0 is 0.
Cannot divide by zero.
Press any key to continue . . .*/
//==========================================================