Thread: Problem with doubles

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    59

    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.
    Last edited by Asbestos; 03-16-2005 at 08:09 PM.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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;
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    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?
    Last edited by Asbestos; 03-16-2005 at 08:07 PM.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You're never actually calling your functions (you just output z without calling multiplication() or division(), etc )
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    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();
    	}
    Last edited by Asbestos; 03-16-2005 at 08:26 PM.

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    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
    }

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    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.

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    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();
    	}
    Last edited by Asbestos; 03-16-2005 at 08:57 PM. Reason: Fixed the extra 'z' variable.

  9. #9

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Oh crap, forgot the bottom doubles part. Okay, but can you help me with the actual problem compiling?

  11. #11
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    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.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    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?

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use the pow() function
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Okay, thank you all so so much. This is truely a great community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. problem with displaying doubles in array
    By Seiro in forum C Programming
    Replies: 5
    Last Post: 04-21-2006, 04:06 PM