0% found this document useful (0 votes)
4 views5 pages

C++ Simple Calculator Program Guide

Uploaded by

Calvin Mkokamasa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

C++ Simple Calculator Program Guide

Uploaded by

Calvin Mkokamasa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++ program typically includes the following elements:

Preprocessor Directives: These are commands that tell the compiler to include certain libraries
before the actual compilation starts.
Main Function: The entry point of every C++ program where execution begins.
Variables: Storage locations for data that the program will use.
Control Structures: These include decision-making statements like if, switch, loops like for,
while, etc.
Input and Output: Mechanisms for interacting with the user.

2. Code Breakdown
Here’s the complete code again for reference:

#include <iostream>
using namespace std;
int main() {
char operation;
float num1, num2;
cout << "Simple Calculator\n";
cout << "Choose an operation:\n";
cout << " + : Addition\n";
cout << " - : Subtraction\n";
cout << " * : Multiplication\n";
cout << " / : Division\n";
cout << "Enter your choice: ";
cin >> operation;
cout << "Enter first number: ";
cin >> num1;
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;
}
1. Preprocessor Directive
#include <iostream>
This line tells the compiler to include the standard input-output stream library, which is
necessary for using cin (to take input) and cout (to print output).
2. Namespace
using namespace std;
This line allows us to use names from the std namespace directly without prefixing them with
std::. For example, we can use cout instead of std::cout.

3. Main Function
int main() {
This defines the main function. It is where the execution of the program starts. The function
returns an integer value, typically 0, to indicate successful execution.

4. Variable Declaration
char operation;
float num1, num2;
char operation: A variable to store the user’s choice of arithmetic operation (e.g., +, -, *, /).
float num1, num2: These variables will hold the two numbers that the user wants to calculate
with. Using float allows for decimal values.

5. Displaying Menu and Taking User Input


cout << "Simple Calculator\n";
cout << "Choose an operation:\n";
cout << " + : Addition\n";
cout << " - : Subtraction\n";
cout << " * : Multiplication\n";
cout << " / : Division\n";
cout << "Enter your choice: ";
cin >> operation;
The program prints a simple menu of operations.
cin >> operation: This line takes input from the user and stores it in the operation variable.

6. Input for Numbers


cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
The program prompts the user to enter two numbers, which are stored in num1 and num2.
7. Switch-Case for Operation Selection
switch (operation) {
The switch statement checks the value of operation and executes the corresponding case block
based on the user’s choice.
Addition (+):
case '+':
cout << "Result: " << num1 + num2 << endl;
break;
If the user chooses +, it calculates the sum of num1 and num2 and prints the result.
Subtraction (-):
case '-':
cout << "Result: " << num1 - num2 << endl;
break;
Similar to addition, but it calculates the difference.
Multiplication (*):
case '*':
cout << "Result: " << num1 * num2 << endl;
break;
This case calculates the product of the two numbers.
Division (/):
case '/':
if (num2 != 0) {
cout << "Result: " << num1 / num2 << endl;
} else {
cout << "Error: Division by zero is not allowed." << endl;
}
break;
Here, it checks if num2 is not zero before performing division. If num2 is zero, it prints an
error message to prevent division by zero, which is mathematically undefined.
Default Case:
default:
cout << "Error: Invalid operation." << endl;
If the user enters an invalid operator, this case is executed, and an error message is displayed.
8. Program Termination
return 0;
This line ends the main() function and returns 0, indicating that the program executed
successfully.

You might also like