I can't seem to find the error in this code. I don't know if I constructed the codes correctly, assuming that the error is corrected, I could get the correct value?

/*  Function for Right Triangle */

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define p printf
#define s scanf

double rightTriangle(double x, double y);

main()
{
      double a,b;
      double c;
p("Input the 2 lengths of the triangle:\n");
p("a="); s("%0.2f", &a);
p("b="); s("%0.2f", &b);
rightTriangle(a,b);
c=sqrt(rightTriangle(a,b));
p ("%0.2f", c);
 getch();
 return 0;
}

double rightTrangle(double x, double y)
{  
return ((x*x) + (y*y));
}

I'm using Dev-C++...someone could make things clear to me, I'd really appreciate it ^_^

Line 9 of you code is

double rightTriangle(double x, double y)

Line 25 of your code is

double rightTrangle(double x, double y)

Notice anything different? I'll give you a hint. Look at the name of the function.

Ohhh I seee....oh man I feel stupid. I thought there was reaally something wrong with the construction of the overall code. Thank you very much for your help! ^____^

Okay. Now that I edited it, here's another problem. The program is running but does not allow me to input the second value and instead, displays a different value. I compared my work to a working program with similar given and...I just couldn't find what's the problem. :/

/*  Function for Right Triangle */

#include<stdio.h>
#include<conio.h>
#include<math.h>


double rightTriangle(double a, double b)
{ 
return sqrt((a*a)+(b*b)) ;
}


main()
{
      double a,b,hyp;

printf("Input the 2 lengths of the triangle:\n"); 
scanf("%0.2E", &a, &b);

hyp=rightTriangle(a,b);
printf ("The hypotenuse of the triangle is: %0.2E", hyp);
 getch();
 return 0;
}

You should be getting a warning on line 19 telling you that you gave two pointers to scanf, but your format string only contains one specifier. If you didn't get that warning, you should increase the warnings level of your compiler.

I note that you're coding in C, but posting in the C++ forum. If you're meaning to code in C++, C++ provides much safer alternatives to printf and scanf.

I didn't get the warning though. But I also tried them separated. The output is still the same. And I noticed that even if I put different initial values "a=", the result doesn't change.

GeneClaude What do you mean by "I also tried them separated"? What did you separate? Please post the code you tried.

PS: If you didn't get a warning, you should increase your compiler's warnings level. Every decent compiler should give a warning when you pass too many or too few arguments to scanf (assuming the format string is constant, which it was in this case).

I mean the line 19...There wasn't any error. and what I meant is that I also tried it like this:

printf("Input the 2 lengths of the triangle:\n");
printf("a=");
scanf("%0.2f", &a);
printf("b=");
scanf(%0.2f", &b);

Yes, that's fine. The problem about the wrong number of arguments should be fixed now.

There wasn't any error.

It would have been a warning - not an error. And as I said: if you didn't get one, increase your warnings level.

Another problem I just noticed is that your variables are doubles, but you're using %E/%f, which expects a float. That would explain why you're still having trouble. Use %lf instead.

Another thing to consider is what should happen if you get bad data. scanf stops if it does not get the type of data it is expecting, and leaves the unexpected characters on the input queue. If your program is expecting a number and you enter a non-numeric character, like k, then scanf will stop, leave the k on the input queue and return, leaving the variable in question in an undefined state. When you try to read again, it will see the invalid character and stop again without asking you for more characters and leaving the invalid character on the input queue.

You should check the return value from scanf and make sure it matches the number of variables you specified it should read. If they do not match, flush the input [ fflush(stdin) ] and try again. Also, for added portability, flush the output [ fflush(stdout) ] before calling the input function.

There can be a problem with specifying the input length like you have. If the number entered is longer than the field size specified, the extra characters are left on the input queue and the next scanf will see these characters and read them in, without asking you for more characters to read.

Edited 3 Years Ago by Nutster: Add new paragraph

This question has already been answered. Start a new discussion instead.