Creating a calculator in C++ is a great exercise for beginners to understand fundamental programming concepts like input/output, conditional statements, and loops. Below is a step-by-step guide, including the full source code for a simple calculator that performs basic arithmetic operations.
#### Step 1: Write the Complete Source File
Here’s the complete code for our calculator:
```cpp
// Simple Calculator in C++
#include
// Include input-output stream header
int main() {
double num1, num2; // Variables to hold the numbers
char operation; // Variable to hold the operation
bool keepGoing = true; // Boolean to control the loop for multiple calculations
// Program loop
while (keepGoing) {
// Prompt user for input
std::cout << "Enter first number: ";
std::cin >> num1;
std::cout << "Enter second number: ";
std::cin >> num2;
// Ask the user for the operation to perform
std::cout << "Choose an operation (+, -, *, /): ";
std::cin >> operation;
// Switch statement to perform the desired operation
switch (operation) {
case '+':
std::cout << "Result: " << num1 + num2 << std::endl;
break;
case '-':
std::cout << "Result: " << num1 - num2 << std::endl;
break;
case '*':
std::cout << "Result: " << num1 * num2 << std::endl;
break;
case '/':
if (num2 != 0) {
std::cout << "Result: " << num1 / num2 << std::endl;
} else {
std::cout << "Error: Division by zero is not allowed." << std::endl;
}
break;
default:
std::cout << "Error: Invalid operation." << std::endl;
break;
}
// Ask the user if they want to perform another calculation
char choice;
std::cout << "Do you want to perform another calculation? (y/n): ";
std::cin >> choice;
if (choice == 'n' || choice == 'N') {
keepGoing = false; // Set loop control variable to false to exit
}
}
return 0; // End of program
}
```
#### Step 2: Explanation of the Code
**1. Including Necessary Headers**:
```cpp
#include
```
- The `#include ` directive includes the standard input-output stream library, which allows the program to use `std::cin` for input and `std::cout` for output.
**2. Main Function**:
```cpp
int main() {
// Code goes here
return 0;
}
```
- The `main()` function is the entry point of the program. The code for the calculator is written inside this function.
**3. Variable Declarations**:
```cpp
double num1, num2;
char operation;
bool keepGoing = true;
```
- `double num1, num2`: These variables store the numbers entered by the user.
- `char operation`: This variable stores the operator chosen by the user (`+`, `-`, `*`, `/`).
- `bool keepGoing = true`: This Boolean variable controls the loop, allowing the user to perform multiple calculations.
**4. Program Loop**:
```cpp
while (keepGoing) {
// Calculation code goes here
}
```
- The `while (keepGoing)` loop allows the program to repeatedly prompt the user for input and perform calculations until the user decides to exit.
**5. User Input**:
```cpp
std::cout << "Enter first number: ";
std::cin >> num1;
std::cout << "Enter second number: ";
std::cin >> num2;
```
- `std::cout` prompts the user to enter values, and `std::cin` reads the input from the user.
**6. Operation Selection**:
```cpp
std::cout << "Choose an operation (+, -, *, /): ";
std::cin >> operation;
```
- The program asks the user to choose an arithmetic operation.
**7. Performing the Operation**:
```cpp
switch (operation) {
case '+':
std::cout << "Result: " << num1 + num2 << std::endl;
break;
case '-':
std::cout << "Result: " << num1 - num2 << std::endl;
break;
case '*':
std::cout << "Result: " << num1 * num2 << std::endl;
break;
case '/':
if (num2 != 0) {
std::cout << "Result: " << num1 / num2 << std::endl;
} else {
std::cout << "Error: Division by zero is not allowed." << std::endl;
}
break;
default:
std::cout << "Error: Invalid operation." << std::endl;
break;
}
```
- The `switch` statement evaluates the `operation` variable and performs the corresponding arithmetic operation.
- For division, an additional check ensures that the denominator (`num2`) is not zero to avoid a runtime error.
**8. Repeating the Calculation**:
```cpp
char choice;
std::cout << "Do you want to perform another calculation? (y/n): ";
std::cin >> choice;
if (choice == 'n' || choice == 'N') {
keepGoing = false;
}
```
- After displaying the result, the program asks if the user wants to perform another calculation. If the user enters 'n' or 'N', the loop terminates, ending the program.
**9. Program Termination**:
```cpp
return 0;
```
- The `return 0;` statement signals the end of the `main()` function and indicates that the program has executed successfully.
#### Step 3: Running the Program
**1. Compile the Program**:
- Open Dev-C++, create a new project, and add your code to the main file.
- Compile the code by pressing `F9` or selecting "Compile & Run" from the Execute menu.
**2. Test the Calculator**:
- After running the program, a console window will open where you can input numbers, select operations, and see the results.
- The program will loop, allowing you to perform multiple calculations until you choose to exit.
#### Conclusion
This simple calculator demonstrates how to handle user input, make decisions using conditional statements, and create loops in C++. You can expand this program by adding more complex operations, error handling, or a graphical user interface (GUI) to enhance the user experience. Happy coding!
0 Comments