Thread: Function not working properly. What am I missing?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    28

    Function not working properly. What am I missing?

    I am currently a student in C, and I'm trying to learn functions. However, the very first demo code the book gave just does not work (I keep getting undefined results). I've triple checked and I have code down exactly, so I'm wondering if they forgot to include a header or if I'm missing something.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    double average (double a, double b)
    {
        return (a + b) / 2;
    }
    
    int main()
    {
        double x, y, z;
        printf("Enter three numbers:");
        scanf("%1g%1g%1g", &x, &y, &z);
        printf("Average of %g and %g: %g\n", x, y, average(x, y));
        printf("Average of %g and %g: %g\n", y, z, average(y, z));
        printf("Average of %g and %g: %g\n", x, z, average(x, z));
    
    
    
        return (0);
    }
    This keeps outputting
    Enter three numbers:234
    Average of 1.8e-307 and 1.8e-307: 1.8e-307
    Average of 1.79046e-307 and 7.96789e+268: 3.98394e+268
    Average of 1.79064e-307 and 7.96789e+268: 3.98394e+268

    Process returned 0 (0x0) execution time : 1.391 s
    Press any key to continue.
    Any ideas?
    Last edited by figarojones; 11-14-2012 at 11:03 AM. Reason: To remove unnecessary code.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What will happen if you divide a double with an int ? You will get an int.This is what you do in the body of your function.This will cut off the decimal digits.In this case you have to return the return value of the function to int.

    If you want to keep having double precision, divide by 2.0 not 2 and change the avg from int to double.

    In case you have get confused with %g see here printf - C++ Reference

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    28
    Actually, avg is the only thing that works (I actually thought I'd removed it, as it is not pertinent to the rest of the code. Sorry about that. It's removed now). I'm talking about how I'm inputting digits that are not being properly output. (i.e. in the first example, '2' is converted to '1.8e-307' and in the third example it's converted to '1.79064e-307' ).
    Last edited by figarojones; 11-14-2012 at 11:00 AM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    1. It says, "Enter three numbers." You're entering "234." That is one number. Separate them with spaces.
    2. The format specifiers for the "scanf()" should be "%lg" - that one in the middle is a lowercase 'l' (ell). It appears you have the number 1 instead.

    [edit] Don't edit the code in your original post - it can make the following responses confusing as they are no longer applicable. Instead, reply with an updated version of your code.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    28
    Thank you, that was it. The font in my book (and my ide) make '1' and 'l' look exactly the same. What was happening was that '234' was getting counted as 3 numbers. It didn't make sense to me, but now it does.

    Thanks again!

    Also, I'll keep not editing in mind.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    That is why, when we see it, we discourage people from using a lower-case ell as a single-letter variable name, as in many fonts it looks similar/identical to the number 1 and/or the capital letter 'I' (eye). I know that you did not do this here, but we do see it from time to time, and it's a good fact to be aware of.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random not working properly
    By KiRa11Love in forum C Programming
    Replies: 7
    Last Post: 08-10-2012, 04:00 AM
  2. Not Working properly
    By Tejas Sanap in forum C Programming
    Replies: 7
    Last Post: 05-10-2011, 06:52 AM
  3. Fscanf not working properly
    By thefinalhero in forum C Programming
    Replies: 9
    Last Post: 10-05-2009, 10:06 AM
  4. Calendar not working properly?
    By kawk in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-19-2007, 01:15 PM
  5. tic tac toe not working properly
    By h_howee in forum Game Programming
    Replies: 2
    Last Post: 01-01-2006, 01:59 AM