Project Digital Calculator in C++
Description de C++ Calculator:
Create a simple console-based digital calculator that can perform addition, subtraction, multiplication, and division operations. The user will input two numbers and select an operation, and the calculator will display the result.
Features:
User Input: Prompt the user to enter two numbers and the operation they want to perform.
Operations: Implement functions for addition, subtraction, multiplication, and division.
Calculation: Based on the selected operation, perform the corresponding calculation on the entered numbers.
Display Result: Output the result of the calculation to the user.
Implementation Steps:
User Input:
*Use cin to get input from the user for the two numbers and the desired operation (e.g., +, -, *, /).
Operations:
*Create separate functions for each arithmetic operation: add, subtract, multiply, and divide.
*Each function should take the two numbers as parameters and return the result of the operation.
Calculation:
*Inside each operation function, perform the corresponding arithmetic operation using the provided numbers.
*For division, make sure to handle cases where division by zero occurs.
Display Result:
*After performing the calculation, use cout to display the result to the user.
User Interface:
*Use a loop to allow the user to perform multiple calculations without restarting the program.
*Display a menu of available operations for the user to choose from.
Sample C++ Calculator Code:
#include
// Function to perform addition
double add(double a, double b) {
return a + b;
}
// Function to perform subtraction
double subtract(double a, double b) {
return a - b;
}
// Function to perform multiplication
double multiply(double a, double b) {
return a * b;
}
// Function to perform division
double divide(double a, double b) {
if (b != 0) {
return a / b;
} else {
std::cout << "Error: Division by zero!" << std::endl;
return 0.0;
}
}
int main() {
double num1, num2;
char operation;
do {
std::cout << "Enter first number: ";
std::cin >> num1;
std::cout << "Enter operation (+, -, *, /): ";
std::cin >> operation;
std::cout << "Enter second number: ";
std::cin >> num2;
double result;
switch (operation) {
case '+':
result = add(num1, num2);
break;
case '-':
result = subtract(num1, num2);
break;
case '*':
result = multiply(num1, num2);
break;
case '/':
result = divide(num1, num2);
break;
default:
std::cout << "Invalid operation!" << std::endl;
continue; // Skip the rest of the loop and start over
}
std::cout << "Result: " << result << std::endl;
char choice;
std::cout << "Do you want to perform another calculation? (y/n): ";
std::cin >> choice;
if (choice != 'y' && choice != 'Y') {
break; // Exit the loop if the user doesn't want to continue
}
} while (true);
std::cout << "Calculator closed." << std::endl;
return 0;
}
Note:- The sample code above provides a basic structure for a digital calculator in C++. You can expand upon this by adding more error handling, improving the user interface, and enhancing the overall functionality.
- This project is a great opportunity to practice using functions, conditional statements, and loops in C++.