In: Computer Science
Write a program to simulate a calculator. Generate two random numbers (between 1-15) and an ask the user for an arithmetic operator. Using a switch statement, your program has to perform the arithmetic operation on those two operands depending on what operator the user entered. Ask the user if he/she wants to repeat the calculations and if the answer is yes or YES, keep repeating. If the answer is something else, then stop after the first calculation. c++
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(NULL));
string choice = "yes";
char ch;
int n1, n2;
while (choice == "yes" || choice == "YES") {
n1 = 1 + (rand() % 15);
n2 = 1 + (rand() % 15);
cout << "Enter operator(+ or - or * or /): ";
cin >> ch;
switch (ch) {
case '+':
cout << n1 << "+" << n2 << "=" << n1+n2 << endl;
break;
case '-':
cout << n1 << "-" << n2 << "=" << n1-n2 << endl;
break;
case '*':
cout << n1 << "*" << n2 << "=" << n1*n2 << endl;
break;
case '/':
cout << n1 << "/" << n2 << "=" << n1/(double)n2 << endl;
break;
default:
cout << "Invalid operator" << endl;
break;
}
cout << "Do you want to try again(yes or YES or something else): ";
cin >> choice;
}
return 0;
}
