-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path4.cpp
42 lines (38 loc) · 907 Bytes
/
4.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
40
41
/*
Create the equivalent of a four-function calculator. The program should ask the user to
enter a number, an operator, and another number. (Use floating point.) It should then
carry out the specified arithmetical operation: adding, subtracting, multiplying, or divid-
ing the two numbers. Use a switch statement to select the operation. Finally, display the
result.
*/
#include<iostream>
using namespace std;
int main(){
double x, y, ans;
char oper, next;
do{
cout << "Enter first number, operator, second number: ";
cin >> x >> oper >> y;
switch(oper){
case '+':
ans = x + y;
break;
case '-':
ans = x - y;
break;
case '*':
ans = x * y;
break;
case '/':
ans = x / y;
break;
default:
ans = 0;
break;
}
cout << "Answer: " << ans;
cout << "\nDo another calculation (y/n): ";
cin >> next;
}while(next == 'y');
return 0;
}