I'm revisiting my simple calculator, and I tried to add doubles. It didn't work out so well. . .
Code:
#include <iostream>
double subtraction ( int x, int y, double z )
{
return z = static_cast< double >( x ) - y;
}
double addition ( int x, int y, double z )
{
return z = static_cast< double >( x ) + y;
}
double multiplication ( int x, int y, double z )
{
return z = static_cast< double >( x ) * y;
}
double division ( int x, int y, double z )
{
return z = static_cast< double >( x ) / y;
}
using std::cout;
using std::cin;
using std::endl;
using std::fixed;
#include <iomanip>
using std::setprecision;
int add();
int sub();
int mul();
int div();
int main()
{
int input, a;
a = 0;
do
{
cout<<"1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Exit\nEnter your choice: ";
cin>> input;
switch ( input )
{
case 1:
add();
break;
case 2:
sub();
break;
case 3:
mul();
break;
case 4:
div();
break;
case 5:
a = ( a + 1 );
break;
default:
cout<<"Error: Invalid Input\n\n";
break;
}
} while ( a != 1 );
cin.get();
}
int sub()
{
int x, y;
double z;
cout<<"Enter the first number to subtract: ";
cin>> x;
cin.ignore();
cout<<"Enter the second number to subtract: ";
cin>> y;
cin.ignore();
cout<<"The difference is: "<<setprecision( 6 ) <<fixed <<z <<endl;
cout<<"\nHit 'Enter' to calculate some more. . .\n";
cin.get();
}
int add()
{
int x, y;
double z;
cout<<"Enter the first number to add: ";
cin>> x;
cin.ignore();
cout<<"Enter the second number to add: ";
cin>> y;
cin.ignore();
cout<<"The sum is: "<<setprecision( 6 ) <<fixed <<z <<endl;
cout<<"\nHit 'Enter' to calculate some more. . .\n";
cin.get();
}
int mul()
{
int x, y;
double z;
cout<<"Enter the first number to be multiplied: ";
cin>> x;
cin.ignore();
cout<<"Enter the second number to be multiplied: ";
cin>> y;
cin.ignore();
cout<<"The product is: "<<setprecision( 6 ) <<fixed <<z <<endl;
cout<<"\nHit 'Enter' to calculate some more. . .\n";
cin.get();
}
int div()
{
int x, y;
double z;
cout<<"Enter the dividend: ";
cin>> x;
cin.ignore();
cout<<"Enter the divisor: ";
cin>> y;
cin.ignore();
cout<<"The quotiant is: "<<setprecision( 6 ) <<fixed <<z <<endl;
cout<<"\nHit 'Enter' to calculate some more. . .\n";
cin.get();
}
Now all the answers (no matter what) are 0.000000. I have no idea why it's doing that, or what I can do to chagne it. My compiler is "DEVC++," if that's any help. . .
EDIT: Updated code and problem.