Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. //============================================================================
  2. // Name : Calculator Program
  3. // Author : Phuc Peter Hoang
  4. // RedID: 821516560
  5. // Data : 9/15/2019
  6. // Version :
  7. // Copyright : Your copyright notice
  8. // Description : Calculator Program with Anonymous Variables. A simple calculator program that repeatedly allows the user
  9. // to select +,-,*,/ from a menu of operations on real values and then enter the two operands which then
  10. // computes the result of applying the selected operation to those operands.
  11. //============================================================================
  12.  
  13. #include <iostream>
  14. #include <limits>
  15. using namespace std;
  16.  
  17. int add (int *a, int *b); //addition function prototype
  18. int subtract (int *c, int *d); //subtraction function prototype
  19. int multiply (int *e, int *f); //multiplication function prototype
  20. int divide (int *g, int *h); //division function prototype
  21. int mod (int *x, int *y); //module function prototype
  22.  
  23. int main() {
  24.  
  25. char *op = new (nothrow) char; //anonymous variable operand user enter
  26. char *choice = new (nothrow) char; //anonymous variable ask user if they want to continue
  27. int *num1 = new (nothrow) int; //anonymous variable first number user enter
  28. int *num2 = new (nothrow )int; //anonymous variable second number user enter
  29. do
  30. {
  31.  
  32. cout << "\nWelcome to the Calculator Program!\n";
  33. cout << "\nEnter the first number:";
  34. cin >> *num1; //input first number
  35.  
  36. while (cin.fail()) //validating user input if they don't enter an integer number
  37. {
  38. cout <<"Error! Please enter an integer value:" <<endl;
  39. cin.clear(); //clear the user's error input
  40. while (cin.get() != '\n'); //ignore the rest of the line after the first instance of user's error
  41. cin >> *num1; //letting user input a true input
  42. }
  43. cout << "\nPlease enter the second number:";
  44. cin >> *num2;
  45.  
  46. while (cin.fail())
  47. {
  48. cout <<"Error! Please enter an integer value:" <<endl;
  49. cin.clear(); //clear the user's error input
  50. while (cin.get() != '\n'); //ignore the rest of the line after the first instance of user's error
  51. cin >> *num2; //letting user input a true input
  52. }
  53. cout << "\nPlease enter the operator you like to perform\n (+, -, *, /): ";
  54. cin >> *op; //input operand
  55.  
  56. while (*op != '+' && *op != '-' && *op != '*' && *op != '/')
  57. {
  58. cout <<"Error! Please enter the four operators: +, -, *, /"<< endl;
  59. cin.clear(); //clear the user's error input
  60. while (cin.get () != '\n'); //ignore the rest of the line after the first instance of user's error
  61. cin >> *op; //letting user input a true input
  62. }
  63.  
  64. if (*op == '+') //addition condition
  65. cout << " " << *num1 << " + " << *num2 << " = " << add (num1, num2) << endl;
  66. else if (*op == '-') //subtraction condition
  67. cout << " " << *num1 << " - " << *num2 << " = " << subtract (num1, num2) << endl;
  68. else if (*op == '*') //multiplication condition
  69. cout << " " << *num1 << " * " << *num2 << " = " << multiply (num1, num2) << endl;
  70. else
  71. {
  72. goback: if (*num2 == 0) //if user input a zero when dividing
  73. {
  74. cout <<"Error! Cannot divide by zero. Please enter a different integer." << endl;
  75. cin.clear (); //clear the user's error input
  76. while (cin.get() != '\n'); //ignore the rest of the line after the first instance of user's error
  77. cin >> *num2; //let user input a non-zero integer value
  78. goto goback; //jumping back to the goback variable so the user can input second number
  79. }
  80. cout << " " << *num1 << " / " << *num2 << " = " << divide (num1, num2) << endl << " Remainder = " << mod (num1, num2) << endl;
  81. }
  82.  
  83. cout <<"\nDo you want to continue? ('y' to continue | 'n' to exit)\n";
  84. cin >> *choice; //user input options of continuing the program or exiting it
  85. }
  86. while (*choice == 'y'); //condition to rerun the program
  87. cout <<"\nEnding Program...";
  88.  
  89. }
  90. int add (int *a, int *b) //addition function
  91. {
  92. int total = 0;
  93. total = *a + *b;
  94. return (total);
  95. }
  96.  
  97. int subtract (int *c, int *d) //subtraction function
  98. {
  99. int total = 0;
  100. total = *c - *d;
  101. return (total);
  102. }
  103.  
  104. int multiply (int *e, int *f) //multiplication function
  105. {
  106. int total = 0;
  107. total = *e * *f;
  108. return (total);
  109. }
  110.  
  111. int divide (int *g, int *h) //division function
  112. {
  113. int total = 0;
  114. total = *g / *h;
  115. return (total);
  116.  
  117. }
  118.  
  119. int mod (int *x, int *y) //remainder function
  120. {
  121. int total = 0;
  122. total = *x % *y;
  123. return (total);
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement