CLI_CALCULATOR_ENGINE is a robust Command-Line Interface calculator application written in C++. This project demonstrates object-oriented programming principles, modular code structure, input validation, and exception handling. It serves as a practical implementation of a console-based calculator with a user-friendly menu system and error-resistant input processing.
CLI_CALCULATOR_ENGINE/
βββ include/
β βββ calculator.h # Calculator class declaration
β βββ input.h # Input validation function declaration
β βββ menu.h # Menu display function declaration
βββ src/
β βββ calculator.cpp # Calculator class implementation
β βββ input.cpp # Input validation implementation
β βββ menu.cpp # Menu display implementation
β βββ main.cpp # Main program logic
βββ build/ # Compiled object files and executable
βββ makefile # Build automation
βββ app.exe # Compiled executable (Windows)
#ifndef CALCULATOR_H
#define CALCULATOR_H
class calculator {
public:
int add(int a, int b);
int sub(int a, int b);
int divide(int a, int b);
int multiply(int a, int b);
int remainder(int a, int b);
};
#endif#ifndef INPUT_H
#define INPUT_H
#include <string>
using namespace std;
int getValidInput(const string& prompt);
#endif#ifndef MENU_H
#define MENU_H
void print();
#endif#include <iostream>
#include <exception>
#include "../include/calculator.h"
#include "../include/menu.h"
#include "../include/input.h"
using namespace std;
int main() {
calculator calc;
while (true) {
print();
int choice = getValidInput("Enter calculation choice : ");
if (choice == 0) {
cout << " ======= Exiting calculator ======= \n ";
break;
}
if (choice < 0 || choice > 5) {
cout << "β Invalid menu choice. Try again.\n";
continue;
}
int a = getValidInput("Enter the value of a : ");
int b = getValidInput("Enter the value of b : ");
bool success = false;
while (!success) {
try {
switch (choice) {
case 1:
cout << "Result == " << calc.add(a, b) << endl;
success = true;
break;
case 2:
cout << "Result == " << calc.sub(a, b) << endl;
success = true;
break;
case 3:
cout << "Result == " << calc.multiply(a, b) << endl;
success = true;
break;
case 4:
cout << "Result == " << calc.divide(a, b) << endl;
success = true;
break;
case 5:
cout << "Result == " << calc.remainder(a, b) << endl;
success = true;
break;
}
}
catch (const exception& e) {
cout << "β οΈ Error: " << e.what() << endl;
cout << "Re-enter the values : " << endl;
a = getValidInput("Enter the value of a : ");
b = getValidInput("Enter the value of b : ");
}
}
}
return 0;
}#include <iostream>
#include "../include/input.h"
using namespace std;
int getValidInput(const string& prompt) {
int x;
while (true) {
cout << prompt;
cin >> x;
if (!cin.fail())
return x;
cout << "Enter valid input " << endl;
cin.clear();
cin.ignore(10000, '\n');
}
}#include <iostream>
#include "../include/menu.h"
using namespace std;
void print() {
cout << endl << "========= Calculator Menu ==========" << endl;
cout << "1. Addition " << endl;
cout << "2. Subtraction " << endl;
cout << "3. Multiplication " << endl;
cout << "4. Division " << endl;
cout << "5. Remainder " << endl;
cout << "0. Exit " << endl;
}- Navigate to the project directory
- Run
maketo compile the project - Execute the program with
./app.exe(Windows) or./app(Linux/Mac)
g++ -c src/calculator.cpp -o build/calculator.o
g++ -c src/input.cpp -o build/input.o
g++ -c src/menu.cpp -o build/menu.o
g++ -c src/main.cpp -o build/main.o
g++ build/*.o -o app.exe- Separated header and implementation files
- Clean separation of concerns (input, calculation, display)
- Easy to extend with new operations
getValidInput()function handles invalid data types- Prevents program crashes from bad input
- Clears input buffer after failures
- Exception handling for mathematical errors
- Graceful recovery from division by zero
- User-friendly error messages
- Addition: Sum of two integers
- Subtraction: Difference between two integers
- Multiplication: Product of two integers
- Division: Integer division with error handling
- Remainder: Modulo operation
- Clear, formatted menu interface
- Continuous operation until explicit exit
- Immediate feedback for invalid choices
========= Calculator Menu ==========
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Remainder
0. Exit
Enter calculation choice : 4
Enter the value of a : 10
Enter the value of b : 2
Result == 5
========= Calculator Menu ==========
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Remainder
0. Exit
Enter calculation choice : 4
Enter the value of a : 10
Enter the value of b : 0
β οΈ Error: Division by zero!
Re-enter the values :
Enter the value of a : 10
Enter the value of b : 3
Result == 3
- Modular Programming: Each component (calculator, input, menu) is isolated
- Separation of Concerns: Clear division between UI, logic, and data handling
- Encapsulation: Calculator operations are bundled in a class
- Stack-based allocation for simplicity
- Proper input stream management with
cin.clear()andcin.ignore() - No dynamic memory allocation required
- Compatible with standard C++11 and above
- Uses header guards to prevent multiple inclusions
- Organized build process with object files
- Integer Only: Currently supports only integer operations
- No History: Calculations aren't stored for future reference
- Basic Operations: Limited to five fundamental operations
- Console Only: No graphical interface available
- Floating-point Support: Add
doubleandfloatoperations - Extended Operations:
- Power/exponentiation
- Square roots
- Trigonometric functions
- Logarithmic operations
- Calculation History:
- Store previous calculations
- Recall and reuse results
- Export to file
- Advanced UI:
- Color-coded output
- Progress indicators
- Batch operation mode
- Configuration Options:
- Precision settings
- Number formatting
- Theme customization
- Testing Suite:
- Unit tests for each operation
- Integration tests
- Performance benchmarks
- Template Support: Generic operations for different numeric types
- Multi-threading: Parallel computation for complex operations
- Plugin Architecture: Loadable operation modules
- Cross-platform GUI: Using frameworks like Qt or NCurses
- Web Interface: REST API for remote calculations
# Compile with debugging symbols
g++ -g -o test_app src/*.cpp -Iinclude
# Run valgrind for memory leaks
valgrind --leak-check=full ./test_app
# Create unit tests for each component
# Example test cases:
# - Division by zero handling
# - Large number calculations
# - Negative number operations
# - Input validation edge cases- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
- Follow C++ Core Guidelines
- Use meaningful variable names
- Add comments for complex logic
- Include error handling for all operations
This project is open-source and available for educational and personal use. Feel free to modify, extend, and distribute with proper attribution.
Deep Lukhi
DSA Enthusiast | C++ Developer | SDE Intern Aspirant
GitHub: https://github.com/deeplukhi
Deep is a passionate programmer with strong interest in Data Structures, Algorithms, and System Design. This project demonstrates practical application of OOP concepts, modular programming, and robust error handling in C++. The CLI Calculator Engine serves as a foundation for more complex computational tools and showcases industry-standard coding practices.
Connect for collaborations, internship opportunities, or technical discussions!
Note: This project is maintained as part of a portfolio to demonstrate C++ programming skills and software engineering principles. The modular architecture allows for easy extension and serves as a template for larger command-line applications.