-
Problem with doubles
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.
-
change the return type of the functions from int to double:
Code:
double subtraction ( int x, int y, double z )
{
return z = static_cast< double >( x ) - y;
}
-
Okay, I'll try it. Thanks!
EDIT: Well, I must have messed up, because now every answer is 0.000000. What is up with that?
-
You're never actually calling your functions (you just output z without calling multiplication() or division(), etc )
-
Oh okay. Thanks again.
EDIT: Yet again, another error. This time I'm getting,
In Function "int sub();" (and all the other ones)
[WARNING] the address of 'double addition'(subtraction, etc. . .), will always evaluate as 'true'
What does that mean, and how can I fix it? Once again, thanks for all the help.
EDIT 2: Here's the updated code.
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 <<subtraction <<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 <<addition <<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 <<multiplication <<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 <<division <<endl;
cout<<"\nHit 'Enter' to calculate some more. . .\n";
cin.get();
}
-
One important thing you should understand (frankly, I'm surprised no one else has mentioned it) is that your variable z is compeletely unnecessary in your functions. Regardless of what you pass into it, you just assign it the result of your calculation. Furthermore, there is no point to assign it anything because it is a local variable.
It's sort of like doing this:
Code:
int foo(int bar) {// bar is not used for anything
return bar = 1;// bar is assigned a value that cannot be used by anyone anywhere
}
-
You are not calling the functions. You are just using the name of the functions. You mean to do "division(x, y, z)" where you are just saying "division". Again, ask yourself what z is doing in those functions.
-
Okay, I fixed the unused variable, but I still have the same compiling error. New, new code is as follows. . .
Code:
#include <iostream>
double subtraction ( int x, int y )
{
return static_cast< double >( x ) - y;
}
double addition ( int x, int y )
{
return static_cast< double >( x ) + y;
}
double multiplication ( int x, int y )
{
return static_cast< double >( x ) * y;
}
double division ( int x, int y )
{
return 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;
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 <<subtraction <<endl;
cout<<"\nHit 'Enter' to calculate some more. . .\n";
cin.get();
}
int add()
{
int x, y;
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 <<addition <<endl;
cout<<"\nHit 'Enter' to calculate some more. . .\n";
cin.get();
}
int mul()
{
int x, y;
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 <<multiplication <<endl;
cout<<"\nHit 'Enter' to calculate some more. . .\n";
cin.get();
}
int div()
{
int x, y;
cout<<"Enter the dividend: ";
cin>> x;
cin.ignore();
cout<<"Enter the divisor: ";
cin>> y;
cin.ignore();
cout<<"The quotiant is: "<<setprecision( 6 ) <<fixed <<division <<endl;
cout<<"\nHit 'Enter' to calculate some more. . .\n";
cin.get();
}
-
-
Oh crap, forgot the bottom doubles part. Okay, but can you help me with the actual problem compiling?
-
Code:
cout<<"The quotiant is: "<<setprecision( 6 ) <<fixed <<division <<endl;
You need to pass the division function (along with the other three) some parameters.
Code:
cout<<"The quotiant is: "<<setprecision( 6 ) <<fixed <<division(x,y) <<endl;
Also, it might not be such a bad idea for your first four functions to take in doubles instead of integers, that way you won't have to worry about casting to double inside them.
-
If your compiler isn't spitting out lots of warnings and errors you need a better compiler.
If it is, try paying attention to what is is saying.
Your most recent missive compiles as follows
Code:
g++ -W -Wall -ansi -pedantic -O2 hello.cpp
hello.cpp: In function `int sub()':
hello.cpp:83: warning: the address of `double subtraction(int, int)', will
always be `true'
hello.cpp:86: warning: control reaches end of non-void function
hello.cpp: In function `int add()':
hello.cpp:97: warning: the address of `double addition(int, int)', will always
be `true'
hello.cpp:100: warning: control reaches end of non-void function
hello.cpp: In function `int mul()':
hello.cpp:111: warning: the address of `double multiplication(int, int)', will
always be `true'
hello.cpp:114: warning: control reaches end of non-void function
hello.cpp: In function `int div()':
hello.cpp:125: warning: the address of `double division(int, int)', will always
be `true'
hello.cpp:128: warning: control reaches end of non-void function
-
Yeah, after having a sudden state of common sense, I realized I needed to add the ( x, y ). Thanks for the help with all of this, but I have just one more question. How do you do exponents? Is there a symbol (^?), or would I have to perform a loop to get it done?
-
-
Okay, thank you all so so much. This is truely a great community.