Thread: Where is the wrong..?

  1. #1
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37

    Where is the wrong..?

    salut..i am trying to make a program in order to calculate the y^2=a^2+b^2 and the code is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    using namespace std;
    
    double ypotinousa(double a, double b); /* function prototype*/
    
    int main(int argc, char *argv[]) /*program begins*/
    
    {
        
     double a,b,y,result;
     
     printf("Give the first number:\n");
     scanf("%d",&a);
     printf("Give the second number:\n");
     scanf("%d",&b);
     
     printf("The result is:%d\n",ypotinousa(a,b));
     
     system("PAUSE");	
     return 0;
     
    }
    
    double ypotinousa(double a, double b)
    {
    double y;
    
    y=pow(a,2)+pow(b,2);
    
    return sqrt(y);
    }
    This program is always return me zero after running..what is wrong? (i guess the function type..)

  2. #2
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    erase result in the code..is wrong

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > using namespace std;
    Get rid of this, this is C++
    You're programming (and posting) C

    > scanf("%d",&a);
    Very bad - the conversion type and variable do not match - you're reading an int into a double
    This does not work at all.

    I suggest you grab the GNU compiler (one of these -> mingw, dev-c++, cygwin, djgpp)
    Then you can do
    gcc -W -Wall -ansi -pedantic -O2 hello.c
    to spot these (and many others) errors

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    also add in
    Code:
    printf("The result is:%d\n",ypotinousa(a,b));
    to the type specifier problem mentioned by salem.

  5. #5
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    so what is the syntax for scanf in order to read double numbers..?

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    29
    scanf("%f", &a);

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or even scanf( "%lf", &mydouble );

    Whilst "%f" is good for both floats and doubles when using printf(), the same is not true for scanf()

    But using gcc with the compile options I posted previously would tell you this...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM