Nested 'if' calculator - division problems
May 3, 2008 at 8:28pm UTC
Here's the problem: we have to program a calculator that can take all sorts of input (M, m, or * for multiply, for example). Everything works perfectly, except for division. For example, first number: 3, second number: 6, output: 0.
Like I said, everything ELSE works fine, and it needs to be nested ifs.
Any help on this would be greatly appreciated
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
//Lab 23 - Flexible Arithmetic Calculator
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
int numOne;
int numTwo;
char mathOp;
int numAdd;
int numSub;
int numMul;
int numDiv;
cout<<"Enter a number: " ;
cin>>numOne;
cout<<"Now enter a second number: " ;
cin>>numTwo;
cout<<"Now, please enter the letter or symbol of the math operation: " ;
cin>>mathOp;
numAdd = numOne + numTwo;
numSub = numOne - numTwo;
numMul = numOne * numTwo;
numDiv = numOne / numTwo;
if (mathOp == 'A' || mathOp == 'a' || mathOp == '+' )
cout<<"The numbers added together are " <<numAdd<<endl;
else
if (mathOp == 'S' || mathOp == 's' || mathOp == '-' )
cout<<"The numbers subtracted are " <<numSub<<endl;
else
if (mathOp == 'M' || mathOp == 'm' || mathOp == '*' )
cout<<"The numbers multiplied are " <<numMul<<endl;
else
if (mathOp == 'D' || mathOp == 'd' || mathOp == '/' )
cout<<"The numbers divided are " <<numDiv<<endl;
else
cout<<"Invalid selection..." <<endl;
getch();
}
Thank you very much.
May 3, 2008 at 8:54pm UTC
change your int variables to double.
May 3, 2008 at 8:56pm UTC
oops stupid mistake
right-o man thanks
Topic archived. No new replies allowed.