Advertisement
aprsc7

Pointer operator

Dec 13th, 2019 (edited)
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void getInput(char*, float*, float*);
  5. void calculate(char*, float*, float*, float*);
  6. void displayResult(char*, float*, float*, float*);
  7.  
  8. int main()
  9. {
  10.     char operation;
  11.     float num1, num2, result;
  12.  
  13.     cout << "MENU\n****\n";
  14.     cout << "+: Add\n-: Subtract\n*: Multiply\n/: Divide\nx: Exit\n";
  15.  
  16.     while(1)
  17.     {
  18.         getInput(&operation, &num1, &num2);
  19.         calculate(&operation, &num1, &num2, &result);
  20.         displayResult(&operation, &num1, &num2, &result);
  21.         cout << endl;
  22.     }
  23. }
  24.  
  25. void getInput(char* ptrOp, float* ptr1, float* ptr2)
  26. {
  27.     cout << "\nEnter your choice: ";
  28.     cin >> *ptrOp;
  29.     if(*ptrOp == 'x' || *ptrOp == 'X')
  30.         exit(1);
  31.  
  32.     cout << "Enter first number: ";
  33.     cin >> *ptr1;
  34.     cout << "Enter second number: ";
  35.     cin >> *ptr2;
  36. }
  37.  
  38. void calculate(char* ptrOp, float* ptr1, float* ptr2, float* ptrResult)
  39. {
  40.     if(*ptrOp == '+')
  41.         *ptrResult = *ptr1 + *ptr2;
  42.  
  43.     else if(*ptrOp == '-')
  44.         *ptrResult = *ptr1 - *ptr2;
  45.  
  46.     else if(*ptrOp == '*')
  47.         *ptrResult = *ptr1 * *ptr2;
  48.  
  49.     else if(*ptrOp == '/')
  50.         *ptrResult = *ptr1 / *ptr2;
  51. }
  52.  
  53. void displayResult(char* ptrOp, float* ptr1, float* ptr2, float* ptrResult)
  54. {
  55.     cout << *ptr1 << " " << *ptrOp << " " << *ptr2 << " = " << *ptrResult;
  56. }
  57. /*
  58.     You should try it sometime.
  59. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement