Thread: passing variables to functions?

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    11

    passing variables to functions?

    my question is, i recieved a float from getData which is stored in cost Sqf. then i send it to the function calculate, and then i print it in the calsulate function and it comes out wrong (i.e. i would type in 22.23 in getData, then in calculate costSqf would = 2.00 when printed)

    how do i send the float to calculate so it would = 22.23, the value that main is holding?

    Code:
    int main (void)
    {
            
            int roomLth;
            int roomWth;
            int custDsc;
            float costSqf;
            float install;
            float subTotal;
            float total;
            
            /*Statements*/
            getData(&roomLth, &roomWth, &custDsc, &costSqf);
            printf("%6.2f is costSqf", costSqf);
            calculate(roomLth, roomWth, custDsc, costSqf, &install,
            &subTotal, &total);
    /*
            printResult(total);
    */
            return 0;
    }
    /*===================================
    calculate function
    =====================================*/
     void calculate (int roomLth,
                            int roomWth,
                            int custDsc,
                            float costSqf,
                            float *install,
                            float *subTotal,
                            float *total)
    {
    
    
    /*statements*/
    
            int a = roomLth;
            int b = roomWth;
            float c = costSqf;
            printf("\n%f is roomWth", costSqf);
            g = calcInstall (a, b, c);
            printf("\n %6.2f test\n", g);
            e = calcSubTotal (g, d);
            printf("%6.2f e \n", e);
    /*      calcTotal (e, &f);
            
            *install = g;
            *subTotal = e;
            *total = f;
    */
    return;
    }
    thanks and if anyone could help it would be appreciated.
    thanks
    Last edited by aoe; 06-02-2002 at 01:31 PM.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    does this statement print the right value in function calculate()?

    printf("\n%f is roomWth", costSqf);

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    11
    no that is the thing, it does not hold the right value

    if in get data for costSqf i typed

    22.23

    in that print statement you just mentioned it would print

    2.00

    here is the code i put into one file, take a look please.

    Code:
    /*chad.h*/
    #include <stdio.h>
    
    /*Prototype Declarations*/
    
    #define lab_Cost    0.35
    #define taxRate     0.085
    
    
    /*main*/
    #include "chad.h"
    
    int main (void)
    {
            int roomLth;
            int roomWth;
            int custDsc;
            float costSqf;
            float install;
            float subTotal;
            float total;
    
            /*Statements*/
            
            getData(&roomLth, &roomWth, &custDsc, &costSqf);
            printf("%6.2f is costSqf", costSqf);
            calculate(roomLth, roomWth, custDsc, costSqf, &install,
            &subTotal, &total);
    /*
            printResult(total);
    */
            return 0;
    }
    
    
    /*getData*/
    void getData (int *roomLth,
                  int *roomWth,
                  int *custDsc,
                  float *costSqf)
    {
            printf("Length of room (feet)?  ");
            scanf("%d", roomLth);
    
            printf("Width of room (feet)?  ");
            scanf("%d", roomWth);
    
            printf("Customer discount (percent)?  ");
            scanf("%d", custDsc);
    
            printf("Cost per square foot (xxx.xx)?  ");
            scanf("%f", costSqf);
    
            printf("%6.2f is correct\n", *costSqf);
    return;
    }
    
    
    /*calc*/
    #include "chad.h"
    /*
            This function calls three subfunctions.
    */
            void calculate (int roomLth,
                            int roomWth,
                            int custDsc,
                            float costSqf,
                            float *install,
                            float *subTotal,
                            float *total)
    {
    
    
    	/*statements*/
    
            int a = roomLth;
            int b = roomWth;
            float c = costSqf;
            int d = custDsc;  
            float e;
            float f;
            float g;
            
            printf("\n %f is costSqf", costSqf);
            g = calcInstall (a, b, c);
            printf("\n %6.2f test\n", g);
            e = calcSubTotal (g, d);
            printf("\n%6.2f test subT \n", e);
    /*      calcTotal (e, &f);
            
            *install = g;
            *subTotal = e;
            *total = f;
    */
    return;
    }
    
    
    
    /*calcI*/
    
    #include "chad.h"
    /*
            This calculates the installed price and cost.
    */
    
            float calcInstall (int x, int y, float z)
    {
            int a;
            float b;
            float c;
            float result;
            printf("\n made it to calcI");
            a = x * y;
            b = z * a;
            c = a * lab_Cost;
            /*result = b + c;
            */
            return (b + c);
    }
    
    
    /*calcS*/
    
    #include "chad.h"
    
    float calcSubTotal (float inst, int dis)
    {
            float mult;
            int hun = 100;
            float ans;
            printf("\n %d is the dis", dis);
            printf("\n%6.2f is install ", inst);
            mult = inst * dis;
            ans = mult;
    
            return ans;
    }
    
    /*calcT*/
    
    #include "chad.h"
    
            void calcTotal (float e, float *f)
    {
    
            float a;
    
            a = e * taxRate;
            *f = a;
            return;
    }
    Last edited by aoe; 06-02-2002 at 01:59 PM.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    11
    sorry, will do next time.

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I'm sorry but this works with my compiler, the right value is printed. I'm using borland c++ builder 5.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    11
    i still can't get it to work for me
    any suggestions?

  7. #7
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Do you receive any warnings when u compile?
    If so, post them, all of them.

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    11
    this is the error i get when i am compiling my code as one file.

    Code:
    silly.c:41: warning: type mismatch with previous implicit declaration
    silly.c:25: warning: previous implicit declaration of `getData'
    silly.c:41: warning: `getData' was previously implicitly declared to return `int'
    silly.c:71: warning: type mismatch with previous implicit declaration
    silly.c:27: warning: previous implicit declaration of `calculate'
    silly.c:71: warning: `calculate' was previously implicitly declared to return `int'
    silly.c:108: warning: type mismatch with previous implicit declaration
    silly.c:85: warning: previous implicit declaration of `calcInstall'
    silly.c:108: warning: `calcInstall' was previously implicitly declared to return `int'
    silly.c:128: warning: type mismatch with previous implicit declaration
    silly.c:87: warning: previous implicit declaration of `calcSubTotal'
    silly.c:128: warning: `calcSubTotal' was previously implicitly declared to return `int'
    515  $
    thanks

    by the way, that is trying to compile through gcc

  9. #9
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    The only advice i can give is :

    1. Check that all the necessary header files are included in all your files.
    2. Try using this program structure :

    //Pre-Processor Directives / Header Files
    ...
    //Funtion Prototypes
    ...
    //main()
    ...
    //Function Definitions
    ...

    3. Check that the return types of your prototypes are the same as the definitions.

    4. Pray for an answer.

    This is 'bout the only advice i can give.

  10. #10
    Registered User
    Join Date
    Jun 2002
    Posts
    11
    i just dont under stand how i can store 3 ints and a float in main using one function, but i can send the 3 int's but not the float to another function?

    it doesn't make sense to me how such an easy task won't work?

    i guess my question is how do i send

    3ints and a float from main to another function?
    Last edited by aoe; 06-02-2002 at 03:24 PM.

  11. #11
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    If you want to change the value of the variables, then

    Code:
    function(int*, int*, int*, float*);
    main()
    {
    function(&int, &int, &int, &float);
    ...
    ...
    }
    if you don't want their values changed, then just take the '*' and the '&' out.

  12. #12
    Registered User
    Join Date
    Jun 2002
    Posts
    11
    thank you for all your help

    Code:
    #include "chad.h"
    int main (void)
    {
            int roomLth;
            int roomWth;
            int custDsc;
            float costSqf;
    
    /*statements*/
    
            getData(&roomLth, &roomWth, &custDsc, &costSqf);
    
            printf("%d = Lth %d = Wth %d = custDsc %7.2f = Sqf\n",  roomLth, roomWth, custDsc,
                    costSqf);
    
            calculate(roomLth, roomWth, custDsc, costSqf); 
    
            return 0;
    }
    this prints the right costSqf in main
    but when i send it to calculate
    the costSqf is not printed correctly.
    if 22.23 is print in main as costSqf
    then when i print from calculate
    2.00 is printed?
    weird?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > silly.c:71: warning: `calculate' was previously implicitly declared to return `int'
    > silly.c:108: warning: type mismatch with previous implicit declaration
    If you're still getting these warnings, then you HAVE to prototype your functions before you call them.

    If there is no prototype, then C makes a GUESS, and in this case, it's guessing wrong.

    Put prototypes for all your functions in chad.h

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing functions between files
    By Duskan in forum C Programming
    Replies: 9
    Last Post: 04-17-2007, 07:44 AM
  2. Variables and Functions
    By Hitetsu in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2006, 08:01 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 11:22 AM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM