Step 1: Create a New Project
Step 2: Coding the Calculator
Replace the template code with the following code to create a basic calculator:
#include <iostream>
using namespace std;
int main() {
char operation;
double num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter an operation (+, -, *, /): ";
cin >> operation;
cout << "Enter second number: ";
cin >> num2;
switch (operation) {
case '+':
cout << "Result: " << num1 + num2 << endl;
break;
case '-':
cout << "Result: " << num1 - num2 << endl;
break;
case '*':
cout << "Result: " << num1 * num2 << endl;
break;
case '/':
if (num2 != 0) {
cout << "Result: " << num1 / num2 << endl;
} else {
cout << "Error: Division by zero is not allowed." << endl;
}
break;
default:
cout << "Error: Invalid operation." << endl;
}
return 0;
}
Step 3: Build and Run the Project
Step 4: Testing the Calculator
Congratulations! You've successfully created a simple calculator program using C++ and Code::Blocks. This mini project helps you practice basic user input, conditional statements, and switch-case statements in C++. Feel free to expand upon this project by adding more functionality or refining the user interface.