oh, no! my function won't work.

This is a discussion on oh, no! my function won't work. within the C Programming forums, part of the General Programming Boards category; I am trying to write a program that will print the smallest of 3 floating-point numbers using a function. I ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    Unhappy 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; 
    }

  2. #2
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Oh, Yes!

    Quote Originally Posted by student0806 View Post
    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.
    Last edited by Char*Pntr; 09-27-2010 at 06:28 PM. Reason: additional suggestion

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    Thanks! I feel silly.

  4. #4
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by student0806 View Post
    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...

    sscanf with double?
    Last edited by Char*Pntr; 09-27-2010 at 06:38 PM. Reason: more info

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  6. #6
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by Adak View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21