0% found this document useful (0 votes)
5 views2 pages

C

This document contains a C++ program for a simple calculator that performs basic arithmetic operations like addition, subtraction, multiplication, and division. It features a menu for user interaction, input validation, and handles division by zero errors. The program runs in a loop until the user chooses to quit by entering 'q'.

Uploaded by

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

C

This document contains a C++ program for a simple calculator that performs basic arithmetic operations like addition, subtraction, multiplication, and division. It features a menu for user interaction, input validation, and handles division by zero errors. The program runs in a loop until the user chooses to quit by entering 'q'.

Uploaded by

sajadpm1232
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <limits> // For numeric limits

// Function declarations
void displayMenu();
void performCalculation(char operation);

int main() {
char operation;

while (true) {
displayMenu();

std::cout << "Enter your choice (or 'q' to quit): ";


std::cin >> operation;

if (operation == 'q') {
std::cout << "Exiting the program. Goodbye!" << std::endl;
break;
}

// Validate operation
if (operation == '+' || operation == '-' || operation == '*' || operation
== '/') {
performCalculation(operation);
} else {
std::cout << "Invalid choice. Please enter a valid operation." <<
std::endl;
}

// Clear input buffer to avoid infinite loop issues


std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

return 0;
}

void displayMenu() {
std::cout << "\nSimple Calculator Menu" << std::endl;
std::cout << "-----------------------" << std::endl;
std::cout << "1. Addition (+)" << std::endl;
std::cout << "2. Subtraction (-)" << std::endl;
std::cout << "3. Multiplication (*)" << std::endl;
std::cout << "4. Division (/)" << std::endl;
std::cout << "Enter 'q' to quit" << std::endl;
}

void performCalculation(char operation) {


double num1, num2;

// Input validation
std::cout << "Enter two numbers: ";
while (!(std::cin >> num1 >> num2)) {
std::cout << "Invalid input. Please enter numerical values: ";
std::cin.clear(); // Clear error flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //
Discard invalid input
}
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 << "Unexpected error." << std::endl;
break;
}
}

You might also like