// Example program
#include <iostream> // Library required for printing
#include <string> // Library required for getting strings and inputs
#include <typeinfo> // Library required for checking a variable data type

float Calculate(float val1, float val2, char OPERATOR) { // Calculates a result from two numeric values and a desired operator
    if (OPERATOR == '+') { // ADDITION
        return val1 + val2; // A + B = C
        
    } else if (OPERATOR == '-') { // SUBTRACTION
        return val1 - val2; // A - B = C
    
    } else if (OPERATOR == '*') { // MULTIPLICATION
        return val1 * val2; // A * B = C
    
    } else if (OPERATOR == '/') { // DIVISION
        if (val2 != 0) { // CHECKS IF DENOMINATOR IS 0
            return val1 / val2; // A / B = C
        } else { // If it is 0
            std::cerr << "Error: Divisoin by zero" << std::endl; // Throw an error and return.
            return 0;
        }
        
    } else if (OPERATOR == '%') { // PERCENTAGE (NOT MODULUS)
        if (val1 != 0) { // CHECKS IF DENOMINATOR IS 0
            return val2 / val1 * 100; // B / A * 100 = C%
        } else { // If it is 0
            std::cerr << "Error: Divisoin by zero" << std::endl; // Throw an error and return.
        }
        
    } else { // IF IT'S NOT AN OPERATOR THAT WAS LISTED, MARK AS INVALID
        std::cerr << "Error: Invalid operator" << std::endl;
        return 0;
    }
}

int main() // Main
{
  std::string c_val1; // Creates the 'c_val1' variable
  std::string c_val2; // Creates the 'c_val2' variable
  char op; // Creates the 'op' variable
  
  std::cout << "Enter a value for 'val1': "; // Asks for the 'c_val1' input
  getline(std::cin, c_val1); // Reads the input
  
  std::cout << "Enter a value for 'val2': "; // Asks for the 'c_val2' input
  getline(std::cin, c_val2); // Reads the input
  
  std::cout << "Enter an operator for 'OPERATOR': "; // Asks for the 'op' input
  std::cin >> op; // Reads the input
  
  float val1, val2; // Create the float variables
  
  try { // Converts the strings to floats using stof().
      val1 = std::stof(c_val1);
      val2 = std::stof(c_val2);
      
  } catch (const std::invalid_argument& e) { // Throw an error if it fails or the entered value wasn't a number.
      std::cerr << "Error: Invalid input. Enter a valid numeric value." << std::endl; // Throw an error for an invalid value (a string/number that wasn't able to be converted to a float)
      return 1; // Didn't execute successfully
  }
  
  float Calculation = Calculate(val1, val2, op); // Creates a variable for the function with the awaiting inputs.
  
  if (op == '%') { // If it's a percentage expression
      std::cout << "Calculation: " << Calculation << "%" << std::endl; // Add a percentage symbol at the end of the result.
  } else { // Otherwise
      std::cout << "Calculation: " << Calculation << std::endl; // Continue with a normal result
  }
  
  return 0; // Always return 0 with the main function.
} // End of main

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: