Input problem when using user-defined functions.

This is a discussion on Input problem when using user-defined functions. within the C Programming forums, part of the General Programming Boards category; I'm scratching my head over this one. My input for feet is always recorded as 0 when I print within ...

  1. #1
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Input problem when using user-defined functions.

    I'm scratching my head over this one. My input for feet is always recorded as 0 when I print within the calc() function, but it is correct when printed in the main() function. What's going on?

    Code:
    #include <stdio.h>
    
    float input (void)
    {
        float x;
    
        scanf("%f", &x);
        getchar();
    
        return x;
    }
    
    float calc ()
    {
        float y, feet, inches;
    
        y = 30.48 * feet + 30.48 * (inches / 12);
    
        printf("%f and %f", feet, inches);
        printf("%f", y);
    
        return y;
    }
    
    void output ()
    {
        int meters;
        float centimeters;
    
        printf("\nThe value is equivalent to %d meters and %.2f centimeters.", meters, centimeters);
    }
    
    int main (void)
    {
        int meters;
        float feet, inches, metric, centimeters;
        char ans;
    
        printf("This program converts a length in feet and inches into meters and centimeters.\n");
    
    do
    {
        printf("Insert length in feet: ");
        feet = input ();
        printf("Insert length in inches: ");
        inches = input ();
    
        metric = calc (feet, inches);
    
        meters = metric / 100;
        centimeters = (metric - meters) * 100;
    
        output (meters, centimeters);
    
        printf("\nWould you like to try again? [Y/N]");
        scanf("%c", &ans);
        getchar();
    
    } while (ans == 'y' || ans == 'Y');
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,396
    In the calc function, the local variables feet and inches were not given an initial value before use.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I'm a bit confused, calc is a function which takes no parameters but your calling it with two parameters from main?

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Thanks! When do we need to initialize the values? Other functions seem to work fine without initialization.

  5. #5
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Quote Originally Posted by ssharish2005 View Post
    I'm a bit confused, calc is a function which takes no parameters but your calling it with two parameters from main?

    ssharish
    From what I read, using () implies unknown number of arguments, whereas having no argument is denoted by (void).

    Correct me if I'm wrong.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,396
    Quote Originally Posted by 843
    From what I read, using () implies unknown number of arguments, whereas having no argument is denoted by (void).
    Yeah, but here you have a function definition, so the number of parameters is known to be zero. If you used those empty parentheses with a function declaration that was not also a function definition, then indeed the number of parameters would be unknown at that point.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    I initialized all the values and rearrange some functions but now I'm getting a bunch of 'incompatible' error messages.

    Code:
    #include <stdio.h>
    
    float input (void)
    {
        float x;
    
        scanf("%f", &x);
        getchar();
    
        return x;
    }
    
    float calc (float feet, float inches)
    {
        return 30.48 * feet + 30.48 * (inches / 12);
    }
    
    int meters (float metric)
    {
        return metric / 100;
    }
    
    float centimeters (float metric, int meters)
    {
        return (metric - meters) * 100;
    }
    
    void output (int meters, float centimeters)
    {
        printf("\nThe value is equivalent to %d meters and %.2f centimeters.", meters, centimeters);
    }
    
    int main (void)
    {
        int m;
        float feet, inches, metric, cm;
        char ans;
    
        printf("This program converts a length in feet and inches into meters and centimeters.\n");
    
    do
    {
        printf("Insert length in feet: ");
        feet = input ();
        printf("Insert length in inches: ");
        inches = input ();
    
        metric = calc (feet, inches);
    
        m = meters (metric);
        cm = centimeters (metric, meters);
    
        output (meters, centimeters);
    
        printf("\nWould you like to try again? [Y/N]");
        scanf("%c", &ans);
        getchar();
    
    } while (ans == 'y' || ans == 'Y');
    
        return 0;
    }
    EDIT: Ah, I see the problem now...
    Last edited by 843; 10-16-2010 at 12:04 PM.

  8. #8
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Quote Originally Posted by laserlight View Post
    ...function declaration that was not also a function definition...
    Could you please clarify this part?

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    >From what I read, using () implies unknown number of arguments, whereas having no argument is denoted by (void).
    Yeah i know that and your right.

    This is interesting, the compiler wouldn't complain if you send parameters to a function which dosn't take any parameters.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  10. #10
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Got it! I didn't realize the functions' arguments are private to those functions only.

    Code:
    #include <stdio.h>
    
    float input (void)
    {
        float x;
    
        scanf("%f", &x);
        getchar();
    
        return x;
    }
    
    float calc (float x, float y)
    {
        return 30.48 * x + 30.48 * (y / 12);
    }
    
    int meters (float x)
    {
        return x / 100;
    }
    
    float centimeters (float x, int y)
    {
        return x - y * 100;
    }
    
    void output (int x, float y)
    {
        printf("\nThe value is equivalent to %d meters and %.2f centimeters.", x, y);
    }
    
    int main (void)
    {
        int m;
        float feet, inches, metric, cm;
        char ans;
    
        printf("This program converts a length in feet and inches into meters and centimeters.\n");
    
    do
    {
        printf("Insert length in feet: ");
        feet = input ();
        printf("Insert length in inches: ");
        inches = input ();
    
        metric = calc (feet, inches);
    
        m = meters (metric);
        cm = centimeters (metric, m);
    
        output (m, cm);
    
        printf("\nWould you like to try again? [Y/N]");
        scanf("%c", &ans);
        getchar();
    
    } while (ans == 'y' || ans == 'Y');
    
        return 0;
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,396
    Quote Originally Posted by 843
    Could you please clarify this part?
    Take your most recent code as an example. You could have written:
    Code:
    float calc();
    
    /* ... */
    
    int main(void)
    {
        /* ... */
    
        metric = calc(feet, inches);
    
        /* ... */
    }
    
    /* ... */
    
    float calc(float x, float y)
    {
        return 30.48 * x + 30.48 * (y / 12);
    }
    But then you might as well just leave the parameters in the function prototype (which is a declaration that is not also a definition).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    What's the point of a prototype since you can just declare the function and its definition?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,396
    Quote Originally Posted by 843
    What's the point of a prototype since you can just declare the function and its definition?
    You may wish to use the function across different source files, so its prototype may go into a header file.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  2. Replies: 14
    Last Post: 03-02-2008, 12:27 PM
  3. Replies: 4
    Last Post: 04-21-2004, 04:18 PM
  4. Help! User defined functions
    By AdrenalinFlow in forum C Programming
    Replies: 3
    Last Post: 02-22-2003, 06:36 PM
  5. Header files
    By borland_man in forum C++ Programming
    Replies: 14
    Last Post: 02-22-2002, 03:30 AM

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