Thread: compile error: implicit return at end of non-void function (warning)...

  1. #1
    Unregistered
    Guest

    compile error: implicit return at end of non-void function (warning)...

    Hi,

    I am using pacific c compiler and get that error when compiling. I also used DEVC++ compiler (which can also compile c programs ) and get the error of illegal operation of program.

    Here is the code, pls help out, and tell me where i went wrong, thnx in advance.



    code:--------------------------------------------------------------------------------
    /* Calculates and prints parking charges. */

    #include <stdio.h>

    float calculateCharges( float );

    int main()
    {
    float timeParked1, timeParked2, timeParked3;

    printf( "Car %2d - hours parked: ", 1 ); /* Car 1 */
    scanf( "%f", timeParked1 );
    printf( "Car %2d - hours parked: ", 2 ); /* Car 2 */
    scanf( "%f", timeParked2 );
    printf( "Car %2d - hours parked: ", 3 ); /* Car 3 */
    scanf( "%f", timeParked3 );

    printf( "%2s%6s%6s", "Car", "Hours", "Charge" );
    printf( "\n%2d%6.2f%6.2f", 1, timeParked1, calculateCharges( timeParked1 ) );
    printf( "\n%2d%6.2f%6.2f", 2, timeParked2, calculateCharges( timeParked2 ) );
    printf( "\n%2d%6.2f%6.2f", 3, timeParked3, calculateCharges( timeParked3 ) );

    return 0;
    }


    float calculateCharges( float parked )
    {
    float fee;

    if ( parked <= 3 )
    return 2.00;
    if ( parked > 3 ) {
    fee = ( parked - 3 ) * 0.50 + 2;
    return fee;
    }
    if ( parked = 24 )
    return 10.00;
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You need an explicit return on the end of your function calculateCharges. If all if-conditions are FALSE, then the function will return nothing. But it should return a float-value.

    This error can also be solved by putting an else-construction after the last if. Some programmers say that after each if there should be an else, so you'll never get these kind of errors.

    And:

    if ( parked = 24 )
    return 10.00;

    Should be:

    if ( parked == 24 )
    return 10.00;

    Else the assignment parked=24 is being evaluated. This will always be true.
    Last edited by Shiro; 01-02-2002 at 09:28 AM.

  3. #3
    Unregistered
    Guest
    i edited the code and fixed a few more bugs, then there's no compile error. But the output is really werid, all the spacing are not correct. The edited code is:

    Code:
    /* Calculates and prints parking charges. */
    
    #include <stdio.h>
    
    float calculateCharges( float );
    
    int main()
    {
       float timeParked1, timeParked2, timeParked3;
    
       printf( "Car %2d - hours parked: ", 1 );   /* Car 1 */
       scanf( "%f", &timeParked1 );
       printf( "Car %2d - hours parked: ", 2 );   /* Car 2 */
       scanf( "%f", &timeParked2 );
       printf( "Car %2d - hours parked: ", 3 );   /* Car 3 */
       scanf( "%f", &timeParked3 );
    
       printf( "%s%6s%6s", "Car", "Hours", "Charge" );
       printf( "\n%d%6f%6.2f", 1, timeParked1, calculateCharges( timeParked1 ) );
       printf( "\n%d%6f%6.2f", 2, timeParked2, calculateCharges( timeParked2 ) );
       printf( "\n%d%6f%6.2f", 3, timeParked3, calculateCharges( timeParked3 ) );
    
       return 0;
    }
    
    
    float calculateCharges( float parked )
    {
       float fee;
    	
       if ( parked <= 3 )
          return 2.00;
       else if ( parked > 3 ) {
          fee = ( parked - 3 ) * 0.50 + 2;
          return fee;
       }
       else if ( parked == 24 )
          return 10.00;
       else
          return 0;
       
    }
    The output of the program is:

    Car - 1 hours parked: 2
    Car - 2 hours parked: Car 3 - hours parked: Car HoursCharge
    1(non-float printf) (n
    2(non-float printf) (n
    3(non-float printf) (n


    pls help thnx

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Your program expects you to put in values for each of three cars. If for some car no input is available, then input 0. This will help. Or, before asking for input, ask the number of cars for which input should be done.

    You should work a little bit on your spaces. The middle row is added onto the number of car, this can be solved by making it %6.2f instead of %6f. And the word Charges has 6 characters, so it will be added on hours. Try 8 instead of 6.

    And there still is a logical error in your program:

    else if (parked > 3)
    {
    fee = ( parked - 3 ) * 0.50 + 2;
    return fee;
    }
    else if ( parked == 24 )
    return 10.00;
    else
    return 0;

    If parked>3, then it will do some calculations and return. But... you will never ever reach parked==24, since 24>3. So if parked==24, then it will go into the if (parked>3) and then return!

    A possible solution:

    float calculateCharges( float parked )
    {
    float fee;

    if ( parked <= 3 )
    flee = 2.00;
    else /* parked > 3 */
    {
    if (parked == 24)
    fee = 10.00;
    else
    fee = (parked - 3) * 0.50 + 2;
    }

    return fee;
    }

  5. #5
    Unregistered
    Guest
    Shiro thnx a lot, problem fixed beautifully.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM