Advertisement
Guest User

Good code

a guest
Oct 21st, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. // Needed header files
  2. #include <iostream>  // Console I/O
  3. #include <string>    // String support
  4. #include <windows.h> // Sleep statement
  5.  
  6. using namespace std; // So I don't have to type std::
  7.  
  8. class App // Application functions
  9. {
  10.     public:
  11.         float getValue() // Get a number from user
  12.         {
  13.             float num;
  14.             cout << "Enter a number..." << endl;
  15.             cin >> num;
  16.             cout << "\n";
  17.             return num;
  18.         }
  19.  
  20.         string getOp()  // Get the operation from user
  21.         {
  22.             string op;
  23.             cout << "What operation do you want to perform?" << endl;
  24.             cin >> op;
  25.             cout << "\n";
  26.             return op;
  27.         }
  28. };
  29.  
  30. class Math // Math operations
  31. {
  32.     public:
  33.         float Add(float a, float b)
  34.         {
  35.             return a + b;
  36.         }
  37.  
  38.         float Subtract(float a, float b)
  39.         {
  40.             return a - b;
  41.         }
  42.  
  43.         float Multiply(float a, float b)
  44.         {
  45.             return a * b;
  46.         }
  47.  
  48.         float Divide(float a, float b)
  49.         {
  50.             return a / b;
  51.         }
  52. };
  53.  
  54. int main()
  55. {
  56.     try  // For errors
  57.     {
  58.         App App;   // App class object
  59.         Math Math; // Math class object
  60.  
  61.         cout << "Welcome to cCalc.\n" << "by: Brendan Gowen\n" << "http://www.github.com/thatoneguy107\n\n";
  62.        
  63.         // Numbers for calculation
  64.         float x = App.getValue();
  65.         float y = App.getValue();
  66.  
  67.         string operation = App.getOp();
  68.  
  69.         if (operation == "add")
  70.         {
  71.             cout << "The answer is: " << Math.Add(x, y);
  72.         }
  73.             cout << "The answer is: " << Math.Add(x, y);
  74.         }
  75.  
  76.         if (operation == "subtract")
  77.         {
  78.             cout << "The answer is: " << Math.Subtract(x, y);
  79.         }
  80.  
  81.         if (operation == "multiply")
  82.         {
  83.             cout << "The answer is: " << Math.Multiply(x, y);
  84.         }
  85.  
  86.         if (operation == "divide")
  87.         {
  88.             cout << "The answer is: " << Math.Divide(x, y);
  89.         }
  90.    
  91.         Sleep(3000); // Allow user to see output
  92.  
  93.         return 0;
  94.     }
  95.    
  96.     catch(...) // Unexpected exception handling
  97.     {
  98.         cout << "Unexpected error." << endl;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement