0% found this document useful (0 votes)
0 views

3rd Assignment

The document describes a simple calculator program that accepts two numbers and an operator, performs the specified arithmetic operation, and displays the result formatted to two decimal places. It utilizes a switch-case statement for operation selection and includes error handling for division by zero and invalid operators. The report also outlines the input handling, processing logic, output formatting, and decisions made during development.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

3rd Assignment

The document describes a simple calculator program that accepts two numbers and an operator, performs the specified arithmetic operation, and displays the result formatted to two decimal places. It utilizes a switch-case statement for operation selection and includes error handling for division by zero and invalid operators. The report also outlines the input handling, processing logic, output formatting, and decisions made during development.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Cassandra Mae S.

Gudio Computer Programming


DEET 2-1 Ms. Roste Mae Macalos

Assignment #3: *CREATING A SIMPLE CALCULATOR* Write a program that acts as a


simple calculator. Accepts two numbers and an operator (+, -, *, /) as input from the user.
Performs the appropriate operation based on the operator entered and displays the result.
Format the output to display the result with two decimal places. Use a switch-case statement
to handle the different operations.

#include <iostream>
#include <iomanip>
using namespace std;

int main(){
cout << "Simple Calculator Program\n";
cout << "-------------------------\n";

double num1, num2, result;


char operation;

cout << "Enter the first number: ";


cin >> num1;
cout << "Enter the operator (+, -, *, /): ";
cin >> operation;
cout << "Enter the second number: ";
cin >> num2;

switch (operation) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
cout << "Error: Division by zero is not allowed.\n";
return 1;
}
break;
default:
cout << "Error: Invalid operator. Please use +, -, *, or /.\n";
return 1;
}
cout << fixed << setprecision(2);
cout << "Result: " << num1 << " " << operation << " " << num2 << " = " << result << endl;

return 0; // Successful execution


}
**Write the Report: ** Along with your code, write a short report (or comments in the code)
describing what each part of your program does and what kind of output it generates. Explain
any decisions you made regarding operator usage and formatting.
This program is a simple calculator that takes arithmetic operators and two numbers as
input, performs the operation, and display the result. It manages the multiplication,
division, addition, and subtraction, with proper handling for division by zero.

1. Input Handling:
The user is asked to provide two numbers and a mathematical operator (+, -, *, or /).
Inputs are read using cin.

2. Processing Logic:
A switch-case statement determines the operation to perform based on the operator:
Addition (+): Adds the two numbers.
Subtraction (-): Subtracts the second number from the first.
Multiplication (*): Multiplies the two numbers.
Division (/): Divides the first number by the second, with a check to ensure the divisor is
not zero.
Invalid operators trigger an error message, and the program exits gracefully.
3. Output Formatting:
The <iomanip> library is used to format the result to two decimal places using fixed and
setprecision(2). This ensures the output is user-friendly and consistent.

4. Error Handling:
Division by zero is explicitly handled with an error message.
Invalid operators are caught, and the program exits with a message indicating the error.

5. Decisions Made:
Use of switch-case:
Chosen for clarity and readability, allowing straightforward addition of new operations if
needed.
Formatting:
The result is displayed with two decimal places to ensure a professional and clean output
format.
Error Handling:
Ensures the program handles common user mistakes (e.g., division by zero, invalid
operator) gracefully without crashing.

6. Example Outputs:
Valid Input:
Input: 5, *, 3
Output: Result: 5.00 * 3.00 = 15.00

7. Division by Zero:
Input: 10, /, 0
Output: Error: Division by zero is not allowed.

8. Invalid Operator:
Input: 4, x, 2
Output: Error: Invalid operator. Please use +, -, *, or /.

You might also like