Thread: Input problem when using user-defined functions.

  1. #1
    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
    28,413
    In the calc function, the local variables feet and inches were not given an initial value before use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,732
    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
    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
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    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
    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,732
    >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
    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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 02:20 PM
  2. Replies: 14
    Last Post: 03-02-2008, 01: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, 07:36 PM
  5. Header files
    By borland_man in forum C++ Programming
    Replies: 14
    Last Post: 02-22-2002, 04:30 AM