forked from thisisshub/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator_C++.cpp
39 lines (39 loc) · 1019 Bytes
/
Calculator_C++.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* A C++ program of a calculator for simple arithmetic operators (+, -,
*, /). The program should take two operands from user and perform the
operation on those two operands depending upon the operator entered by
user. */
#include<iostream>
using namespace std;
int main(){
cout<<"\nWelcome to Calculator.\n Please enter data in the following format:-\n";
cout<<"(number1)(space)(operator)(space)(number2) \n";
float number1,number2,number3;
char op,a;
do{
cin>>number1>>op>>number2;
switch(op){
case '+':
number3=number1+number2;
break;
case '-':
number3=number1-number2;
break;
case '*':
number3=number1*number2;
break;
case '/':
if(number2==0)
{cout<<"Numbers invalid for operation, please try again.";
break;}
else
{number3=number1/number2;
break;}
default:
cout<<"Operator not recognised. Please try again.";
}
cout<<"Answer: \t"<<number3<<endl;
cout<<"Do you want to try again?(y/n)\n";
cin>>a;
}while(a=='y');
return 0;
}