Thread: really simple question

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    18

    really simple question

    i am trying to return the result "c" back to the main function. can anyone see why this isn't printing the correct answer the second time?

    thanks, jez

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int divide(float c);
    
    
    int main()
    {
        float fraction;
    
        divide(fraction);
        printf("%f",fraction);
    
    return 0;
    }
    
    int divide(float c)
    {
        float a, b;
    
        scanf("%f",&a);
        scanf("%f",&b);
        c=a/b;
        printf("%f\n",c);
        return(c);
    }

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    initialize fraction with a value in main and also return 0 at the end of main().u're also not saving the returned vlaue from divide in some variable in main().
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    18
    Quote Originally Posted by BEN10 View Post
    initialize fraction with a value in main and also return 0 at the end of main().u're also not saving the returned vlaue from divide in some variable in main().
    sorry mate i don't understand. do u mean this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int divide(float c);
    
    
    int main(float fraction)
    {
    
    
        divide(fraction);
        printf("%f",fraction);
    
        return 0;
    }
    
    int divide(float c)
    {
        float a, b;
    
        scanf("%f",&a);
        scanf("%f",&b);
        c=a/b;
        printf("%f\n",c);
        return(c);
    }
    because it doesn't work either!

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Simple answer to your original question: yes.

    Just in case you really want a slightly more illuminating response, I'll point out that the function divide accepts its argument by value. Which means that the call divide(fraction) passes a copy of the value of fraction. The changes of c within divide() are local to that function, and therefore not visible in main().

    Two (of many) options to do what you want are;
    Code:
    include <stdio.h>
    #include <stdlib.h>
    
    float divide();
    
    int main()
    {
        float fraction;
    
        fraction = divide();
        printf("%f",fraction);
    
    return 0;
    }
    
    float divide()
    {
        float a, b;
    
        scanf("%f",&a);
        scanf("%f",&b);
        c=a/b;
        printf("%f\n",c);
        return(c);
    }
    or (passing pointers around, not values).
    Code:
    include <stdio.h>
    #include <stdlib.h>
    
    int divide(float *c);
    
    int main()
    {
        float fraction;
    
        divide(&fraction);
        printf("%f",fraction);
    
    return 0;
    }
    
    int divide(float *c)
    {
        float a, b;
    
        scanf("%f",&a);
        scanf("%f",&b);
        *c=a/b;
        printf("%f\n",*c);
        return(*c);   / *I won't even speculate why you're converting the value you calculate to an int */
    }

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    No.i mean in the line where u declare fraction in main() do this.
    Code:
    float fraction=0.0;//anything here to initialize
    also do this to print the correct value.
    Code:
    printf("%d",divide(fraction));
    Last edited by BEN10; 04-25-2009 at 07:16 AM.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    18
    thanks for the replys guys.

    "/ *I won't even speculate why you're converting the value you calculate to an int */"

    I thought "int" was how you defined a function!?

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by JezW View Post
    I thought "int" was how you defined a function!?
    No. That's how you tell the compiler: the function returns an integer. You can change it to any type.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    18
    thanks guys i know this is really basic stuff but i do appreciate it and its very helpful

    Jez

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    18
    The program i am writing takes in 6 strings a0,b0,c0,x0,y0 and z0. It then converts them to doubles a,b,c,x,y,z using the atof function. if the user types "f" then it goes to the divide function that returns a float so there is no need for the atof.

    I am trying to create a for loop to cut down unnecessary code

    Instead of:

    Code:
        printf("A = ");
        scanf("%s",a0);
    
        if (strcmpi("f",a0)==0)
            {
                a=divide();
            }
        else
            {
                a=atof(a0);
            }
    For a,b,c,x,y,z^^^^^

    I would like something like this:

    Code:
        for (i=0;i<6;i++)
    
            {
            if (i==0) {
                      sprintf(variable,'a');
                      sprintf(variable0,'a0');
                      }
            if (i==1) {
                      sprintf(variable,'b');
                      sprintf(variable0,'b0');
                      }
            if (i==2) {
                      sprintf(variable,'c');
                      sprintf(variable0,'c0');
                      }
            if (i==3) {
                      sprintf(variable,'x');
                      sprintf(variable0,'x0');
                      }
            if (i==4) {
                      sprintf(variable,'y');
                      sprintf(variable0,'y0');
                      }
            if (i==5) {
                      sprintf(variable,'z');
                      sprintf(variable0,'a0');
                      }
    
            printf("\n                                      %s = ", variable);
            scanf("%s",variable0);
    
            if (strcmpi("f",*variable0)==0)
                {
                    *variable=divide();
                }
            else
                {
                    *variable=atof(variable0);
                }
    
            }

    I know the code is quite bad but hopefully you can see what i am trying to do. I am literally trying to replace the word "variable" with a,b,c,x,y,z and i know it won't work. I had a go with pointers too - it got the program running but it crashes straight away.


    Thanks for all the help,

    Jez
    Last edited by JezW; 04-25-2009 at 07:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM