Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : Calculator Program
- // Author : Phuc Peter Hoang
- // RedID: 821516560
- // Data : 9/15/2019
- // Version :
- // Copyright : Your copyright notice
- // Description : Calculator Program with Anonymous Variables. A simple calculator program that repeatedly allows the user
- // to select +,-,*,/ from a menu of operations on real values and then enter the two operands which then
- // computes the result of applying the selected operation to those operands.
- //============================================================================
- #include <iostream>
- #include <limits>
- using namespace std;
- int add (int *a, int *b); //addition function prototype
- int subtract (int *c, int *d); //subtraction function prototype
- int multiply (int *e, int *f); //multiplication function prototype
- int divide (int *g, int *h); //division function prototype
- int mod (int *x, int *y); //module function prototype
- int main() {
- char *op = new (nothrow) char; //anonymous variable operand user enter
- char *choice = new (nothrow) char; //anonymous variable ask user if they want to continue
- int *num1 = new (nothrow) int; //anonymous variable first number user enter
- int *num2 = new (nothrow )int; //anonymous variable second number user enter
- do
- {
- cout << "\nWelcome to the Calculator Program!\n";
- cout << "\nEnter the first number:";
- cin >> *num1; //input first number
- while (cin.fail()) //validating user input if they don't enter an integer number
- {
- cout <<"Error! Please enter an integer value:" <<endl;
- cin.clear(); //clear the user's error input
- while (cin.get() != '\n'); //ignore the rest of the line after the first instance of user's error
- cin >> *num1; //letting user input a true input
- }
- cout << "\nPlease enter the second number:";
- cin >> *num2;
- while (cin.fail())
- {
- cout <<"Error! Please enter an integer value:" <<endl;
- cin.clear(); //clear the user's error input
- while (cin.get() != '\n'); //ignore the rest of the line after the first instance of user's error
- cin >> *num2; //letting user input a true input
- }
- cout << "\nPlease enter the operator you like to perform\n (+, -, *, /): ";
- cin >> *op; //input operand
- while (*op != '+' && *op != '-' && *op != '*' && *op != '/')
- {
- cout <<"Error! Please enter the four operators: +, -, *, /"<< endl;
- cin.clear(); //clear the user's error input
- while (cin.get () != '\n'); //ignore the rest of the line after the first instance of user's error
- cin >> *op; //letting user input a true input
- }
- if (*op == '+') //addition condition
- cout << " " << *num1 << " + " << *num2 << " = " << add (num1, num2) << endl;
- else if (*op == '-') //subtraction condition
- cout << " " << *num1 << " - " << *num2 << " = " << subtract (num1, num2) << endl;
- else if (*op == '*') //multiplication condition
- cout << " " << *num1 << " * " << *num2 << " = " << multiply (num1, num2) << endl;
- else
- {
- goback: if (*num2 == 0) //if user input a zero when dividing
- {
- cout <<"Error! Cannot divide by zero. Please enter a different integer." << endl;
- cin.clear (); //clear the user's error input
- while (cin.get() != '\n'); //ignore the rest of the line after the first instance of user's error
- cin >> *num2; //let user input a non-zero integer value
- goto goback; //jumping back to the goback variable so the user can input second number
- }
- cout << " " << *num1 << " / " << *num2 << " = " << divide (num1, num2) << endl << " Remainder = " << mod (num1, num2) << endl;
- }
- cout <<"\nDo you want to continue? ('y' to continue | 'n' to exit)\n";
- cin >> *choice; //user input options of continuing the program or exiting it
- }
- while (*choice == 'y'); //condition to rerun the program
- cout <<"\nEnding Program...";
- }
- int add (int *a, int *b) //addition function
- {
- int total = 0;
- total = *a + *b;
- return (total);
- }
- int subtract (int *c, int *d) //subtraction function
- {
- int total = 0;
- total = *c - *d;
- return (total);
- }
- int multiply (int *e, int *f) //multiplication function
- {
- int total = 0;
- total = *e * *f;
- return (total);
- }
- int divide (int *g, int *h) //division function
- {
- int total = 0;
- total = *g / *h;
- return (total);
- }
- int mod (int *x, int *y) //remainder function
- {
- int total = 0;
- total = *x % *y;
- return (total);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement