C++ 4 function calculator
Sep 24, 2010 at 1:22am UTC
done
Last edited on Sep 30, 2010 at 10:34pm UTC
Sep 24, 2010 at 1:31am UTC
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#include <iostream>
using namespace std;
#include <stdlib.h>
int main ()
{
double D1, D2 , D3 (0);
char Operator;
const bool T(true ), F(false );
do
{
cout <<"\nEnter the first number:" ;
cin>>D1;
cout << "\nPlease enter an operator (+ - * / or type X to exit or C to restart): " << endl;
cin >> Operator;
} while (Operator == 'C' );
while (!F)
{
switch (Operator)
{
case '+' :
cout<<"Enter second number : " ;
cin>>D2;
D3=D1+D2;
cout<< D1<< " + " << D2 << " = " << D3 << endl;
T;
break ;
case '-' :
cout<<"Enter second number : " ;
cin>>D2;
D3=D1-D2;
cout<< D1<< " - " << D1 << " = " << D3 << endl;
T;
break ;
case '*' :
cout<<"Enter second number : " ;
cin>>D2;
D3=D1*D2;
cout<< D1<< " * " << D2 << " = " << D3 << endl;
T;
break ;
case '/' :
cout<<"Enter second number : " ;
cin>>D2;
if ( D2==0)
{
cout << "Undefined answer(s). Enter new divisor.." << endl;
cin >> D2;
}
else D3= D1/D2;
cout << D1 << " / " << D2 << " = " << D3 << endl;
T;
break ;
case 'C' :
case 'c' :
D1=0;
cout << " Enter first number " ;
cin>>D1;
T;
break ;
case 'X' :
case 'x' :
T;
exit (0) ;
break ;
default :
F;
cout << "Invalid response " << Operator << " - Please enter a valid operation - Enter X to quit or enter C to clear" << endl;
}
D1=D3;
cout << "Enter new operator" << endl;
}
}
Just a simple do...while loop.
Sep 24, 2010 at 3:05am UTC
---
Last edited on Sep 30, 2010 at 10:40pm UTC
Sep 24, 2010 at 3:10am UTC
How about using continue
after asking for the first value in the case to jump to the start of a loop, prompting to have it ask for the second value and the operator without changing D1?
-Albatross
Sep 24, 2010 at 3:18am UTC
Holy shit, I love you.
Sep 24, 2010 at 1:24pm UTC
Wow there is some nasty stuff in the code.
Sep 24, 2010 at 4:47pm UTC
I know it's really broken, what do you recommend?
Sep 24, 2010 at 7:25pm UTC
A simple tip is to name your variables something much more distinct. There other things you could do but that would be a big plus in itself.
Topic archived. No new replies allowed.