Skip to content

Commit cd8fa28

Browse files
authored
Initial commit
1 parent e6d43df commit cd8fa28

1 file changed

Lines changed: 126 additions & 0 deletions

File tree

Initial.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include <iostream> // Include input/output stream library
2+
#include <cmath> // Include math library for advanced operations
3+
#include <limits> // Include limits library to handle invalid input
4+
5+
// Function prototypes
6+
void displayMenu();
7+
double getNumber(const std::string& prompt);
8+
char getOperation();
9+
double calculate(double num1, double num2, char op);
10+
bool shouldContinue();
11+
12+
int main() {
13+
std::cout << "Simple Calculator Program\n";
14+
std::cout << "=======================\n";
15+
16+
bool continueCalculation = true;
17+
18+
// Main program loop — continues until user decides to exit
19+
while (continueCalculation) {
20+
displayMenu();
21+
22+
double firstNumber = getNumber("Enter the first number: ");
23+
double secondNumber = getNumber("Enter the second number: ");
24+
char operation = getOperation();
25+
26+
// Perform calculation and display result
27+
double result = calculate(firstNumber, secondNumber, operation);
28+
std::cout << "Result: " << firstNumber << " " << operation
29+
<< " " << secondNumber << " = " << result << "\n\n";
30+
31+
// Ask user if they want to perform another calculation
32+
continueCalculation = shouldContinue();
33+
}
34+
35+
std::cout << "Thank you for using the calculator. Goodbye!\n";
36+
return 0;
37+
}
38+
39+
// Displays the available operations to the user
40+
void displayMenu() {
41+
std::cout << "Available operations:\n";
42+
std::cout << "+ : Addition\n";
43+
std::cout << "- : Subtraction\n";
44+
std::cout << "* : Multiplication\n";
45+
std::cout << "/ : Division\n";
46+
std::cout << "^ : Power (first number raised to the power of second number)\n";
47+
std::cout << "--------------------------------\n";
48+
}
49+
50+
// Prompts the user to enter a number and validates the input
51+
double getNumber(const std::string& prompt) {
52+
double number;
53+
while (true) {
54+
std::cout << prompt;
55+
std::cin >> number;
56+
57+
// Check if input is valid (not a string or special character)
58+
if (std::cin.fail()) {
59+
std::cin.clear(); // Clear the error flag
60+
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Ignore invalid input
61+
std::cout << "Invalid input. Please enter a valid number.\n";
62+
} else {
63+
break; // Valid number entered, exit loop
64+
}
65+
}
66+
return number;
67+
}
68+
69+
// Gets the arithmetic operation from the user and validates it
70+
char getOperation() {
71+
char operation;
72+
while (true) {
73+
std::cout << "Enter the operation (+, -, *, /, ^): ";
74+
std::cin >> operation;
75+
76+
// Validate operation input
77+
if (operation == '+' || operation == '-' || operation == '*' ||
78+
operation == '/' || operation == '^') {
79+
break;
80+
} else {
81+
std::cout << "Invalid operation. Please enter one of: +, -, *, /, ^\n";
82+
}
83+
}
84+
return operation;
85+
}
86+
87+
// Performs the specified arithmetic operation on two numbers
88+
double calculate(double num1, double num2, char op) {
89+
switch (op) {
90+
case '+':
91+
return num1 + num2;
92+
case '-':
93+
return num1 - num2;
94+
case '*':
95+
return num1 * num2;
96+
case '/':
97+
if (num2 != 0) {
98+
return num1 / num2;
99+
} else {
100+
std::cout << "Error: Division by zero is not allowed.\n";
101+
return 0; // Return 0 in case of division by zero
102+
}
103+
case '^':
104+
return std::pow(num1, num2); // Use pow function for exponentiation
105+
default:
106+
std::cout << "Unknown operation.\n";
107+
return 0;
108+
}
109+
}
110+
111+
// Asks the user whether to continue with another calculation
112+
bool shouldContinue() {
113+
char choice;
114+
while (true) {
115+
std::cout << "Do you want to perform another calculation? (y/n): ";
116+
std::cin >> choice;
117+
118+
if (choice == 'y' || choice == 'Y') {
119+
return true;
120+
} else if (choice == 'n' || choice == 'N') {
121+
return false;
122+
} else {
123+
std::cout << "Please enter 'y' for yes or 'n' for no.\n";
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)