# oh, no! my function won't work.

Printable View

• 09-27-2010
student0806
oh, no! my function won't work.
I am trying to write a program that will print the smallest of 3 floating-point numbers using a function. I just learned how to do this function business today... so I'm pretty confused. Can anyone tell me what I did wrong?

Code:

```#include <stdio.h> #include <stdlib.h> float minimum( float x, float y, float z ); int main( void ) { float num1, num2, num3; printf( "Enter 3 numbers.\n" ); scanf( "%.2f%.2f%.2f", &num1, &num2, &num3 ); printf( "Minimum is %.2f\n", minimum( num1, num2, num3 ) ); system("pause"); return 0; } float minimum( float x, float y, float z ) { float min; if((x < y ) && ( x < z )) { min = x; } if((y < x ) && ( y < z )) { min = y; } if((z < x ) && ( z < y )) { min = z; } return min; }```
• 09-27-2010
Char*Pntr
Oh, Yes!
Quote:

Originally Posted by student0806
I am trying to write a program that will print the smallest of 3 floating-point numbers using a function. I just learned how to do this function business today... so I'm pretty confused. Can anyone tell me what I did wrong?

Code:

```#include <stdio.h> #include <stdlib.h> float minimum( float x, float y, float z ); int main( void ) { float num1, num2, num3; printf( "Enter 3 numbers.\n" ); scanf( "%.2f%.2f%.2f", &num1, &num2, &num3 ); printf( "Minimum is %.2f\n", minimum( num1, num2, num3 ) ); system("pause"); return 0; } float minimum( float x, float y, float z ) { float min; if((x < y ) && ( x < z )) { min = x; } if((y < x ) && ( y < z )) { min = y; } if((z < x ) && ( z < y )) { min = z; } return min; }```

Tweak your scanf() in main() to look like this:

Code:

`scanf( "%f %f %f", &num1, &num2, &num3 );`
The FAQ here can fill you in on scanf() requirements. I tested it and it works.

I have no comment on any other code.

edit: I should suggest that you indent your code, it will help you & us to easily see the flow as your
programs get larger.
• 09-27-2010
student0806
Thanks! I feel silly.
• 09-27-2010
Char*Pntr
Quote:

Originally Posted by student0806
Thanks! I feel silly.

No, don't! Right now I looking at my C - A Reference Manual, 5th ed.

And I'm on page 378. Lots of complicate stuff there on scanf()

I'm studying it now, and I'll get back to as why your %.2f that you
used caused a crash.

What's worrying me is there does exist a "specified maximum field width"

Ill get back later.

***************

OK I found what we were looking for. It's a lot quicker for me to search the FAQ
than reading that reference book.

PI is the one that gives the answer to that. The post is I think 2005, and nobody corrected
him so...

http://cboard.cprogramming.com/c-pro...nf-double.html
• 09-27-2010
Adak
The .2 before the f in scanf() is a precision specifier, not a field width. AFAIK, printf() has the precision specifier, but scanf() doesn't.
• 09-27-2010
Char*Pntr
Quote:

Originally Posted by Adak
The .2 before the f in scanf() is a precision specifier, not a field width. AFAIK, printf() has the precision specifier, but scanf() doesn't.

Thanks Adak! That seems to be the consensus of one old thread. I have the
reference book mentioned above.. it's a case of too much information (511) on scanf()
of several pages of technical detail.

I prefer, "just the facts, ma'am" such as the brief details found on this website.