Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Trying to make a program to take and print the natural log of a number input

  1. Dec 8, 2011 #1
    I'm relatively new to C programming and I am trying to make a program to take and print the natural log of a number input by the user but for some reason my program always returns incorrect answers. For example I entered 1 as my number, and the answer came back as some ridiculously big value which is clearly wrong as I know that ln 1 = 0.
    My source code is given below, any help would be greatly appreciated :)

    Code (Text):

    #include <stdio.h>
    #include <math.h>

    int main(void)
    {
        float in;
        float out;    
        do {
       
        printf("Enter the number to take ln of...\n");
        scanf("%f", &in);
        out = log(in);
        printf("Ln of %f is %f\n", &in, &out);
        printf("If you want to do this again press 1, if not press any other number\n");
        getchar();}
        while(1==1);
    }
     
     
    Last edited: Dec 8, 2011
  2. jcsd
  3. Dec 8, 2011 #2

    Borek

    User Avatar

    Staff: Mentor

    Re: Does anyone know why this doesn't work? (C programming)

    What & means?

    Use [noparse]
    Code (Text):
     
    [/noparse] tags to format your source code:

    Code (Text):
    #include <stdio.h>
    #include <math.h>

    int main(void)
    {
       float in;
       float out;
       do {
          printf("Enter the number to take ln of...\n");
          scanf("%f", &in);
          out = log(in);
          printf("Ln of %f is %f\n", &in, &out);
          printf("If you want to do this again press 1, if not press any other number\n");
          getchar();
       } while(1==1);
    }
     
  4. Dec 8, 2011 #3
    The & sign is how my compiler knows what variable i want to use for %f etc.
     
  5. Dec 8, 2011 #4

    Borek

    User Avatar

    Staff: Mentor

    Not exactly. & doesn't mean what variable, & means where the variable is. It is necessary when you use scanf, as scanf needs a reference to be able to put the value in the variable, but printf needs just a value.
     
  6. Dec 8, 2011 #5
    Ok, like I said Im relatively new, but I removed those & signs from printf and it works fine now, thanks :)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Trying to make a program to take and print the natural log of a number input
  1. Make a program (Replies: 2)

  2. Taking inputs in C (Replies: 11)

Loading...