Thread: function calls and pointers

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    36

    function calls and pointers

    I got this compiling but I'm getting no values in my output. Can anyone help out and tell me what I'm doing wrong.
    Code:
     
    #include <stdio.h>
    #include <time.h>
    #define SIZE 5
    
    /* function prototypes */
    double convert(int money);
    
    int main()
    {
       /* variable and array declarations */
       int money;
       srand( time(NULL));
       
       float xchange[] = { 1.00, 1.3593, 1.0013, 83.1300, 1.5837, rand(time)%100 };
          
    	/*
    	 * As of January 30, 2011, the current exchange values from www.google.com/finance.
    	 * USD (United States Dollar) = 1.00
    	 * EUR (Euro Dollar) = 1.3593
    	 * CAD (Canadian Dollar) = 1.0013
    	 * JPY (Japanese Yen) = 83.1300
    	 * GBP (Great Britian Pound) = 1.5837
    	 * DDS (Davidian Dollar Standard) = 3.56
    	 */
    
       /* prompt the user to enter a US dollar amount */
       printf( "Please enter the number of US dollar to be converted:\n" );
       scanf("%d.2f", money);
    
    	convert(money); /* function call */
    
    return 0; /* indicates successfull termination */
    } /* end main function */
    
    /* function prototypes */
    double convert(int money)
    
    {
    
       int *Ptr0, *Ptr1, *Ptr2, *Ptr3, *Ptr4, *Ptr5;
       int USD, EUR, CAD, JPY, GBP, DDS;
       Ptr0 = &USD;
       Ptr1 = &EUR;
       Ptr2 = &CAD;
       Ptr3 = &JPY;
       Ptr4 = &GBP;
       Ptr5 = &DDS;
    
       /* print the converted foreign currencies to the screen */
       *Ptr0 = 1.00 * money; /* USD */
       printf( "The total amount in US dollars: %8.2f\n", &USD);
    
       *Ptr1 = 1.3593 * money; /* EUR */
       printf( "The total amount in Euro dollars: %8.2f\n", &EUR);
    
       *Ptr2 = 1.0013 * money; /* CAD */
       printf( "The total amount in Canadian dollars: %8.2f\n", &CAD);
    
       *Ptr3 = 83.1300 * money; /* JPY */
       printf( "The total amount in Japanese Yen: %8.2f\n", &JPY);
    
       *Ptr4 = 1.5837 * money; /* GBP */
       printf( "The total amount in British Pounds: %8.2f\n", &GBP);
    
       *Ptr5 = rand(time)%100 * money; /* DDS */
       printf( "The total amount in Davidian dollars: %8.2f\n", &DDS);
    
    return 0; /* indicates successful termination */
    } /* end function money */
    Code:
    My output:
    
    
    Please enter the number of US dollar to be converted:
    1
    The total amount in US dollars:     0.00
    The total amount in Euro dollars:     0.00
    The total amount in Canadian dollars:     0.00
    The total amount in Japanese Yen:     0.00
    The total amount in British Pounds:     0.00
    The total amount in Davidian dollars:     0.00
    Last edited by davidjg; 01-30-2011 at 09:46 PM.

  2. #2
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    any problems?

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    /* function prototypes */
    void currency ( int *Ptr)
    {
       /* Do something here */
    }
    
    double convert(int money)
    
    {
    
       int *Ptr0, *Ptr1, *Ptr2, *Ptr3, *Ptr4, *Ptr5;
    Edit: Since you edited your post, now I have to edit mine... Basically, what you asked about initially should get fixed if you do something similar to what I showed in the above code. Now you just have to deal with all the other problems.
    Last edited by kermit; 01-30-2011 at 09:44 PM.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    Sorry for editing so quickly and thanks for your reply. I got it compiling but I can't seem to get the function call to read the values from the array in main. Any ideas on what to do here?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'll have to pass that array to your function, just like you passed the input value money into your function.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    @tabstop.....I totally agree but how would I do that?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The same way you passed your variable "money" to the function.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    I'm just a C newb. The way that moeny was passed is through a function call "convert(money);" Can you provide an example on how I would pass the values of the array into the function? Help please!

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    convert(money, other_things, those_things_over_here, change_under_the_sofa_cushions)

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    Ok.......it's still giving me no value in the output. Any ideas on what to do here?

    convert(money); /* function call */
    convert(xChange); /* function call */



    Please enter the number of US dollars to be converted:
    1
    The total amount in US dollars: 0.00
    The total amount in Euro dollars: 0.00
    The total amount in Canadian dollars: 0.00
    The total amount in Japanese Yen: 0.00
    The total amount in British Pounds: 0.00
    The total amount in Davidian dollars: 0.00
    The total amount in US dollars: 0.00
    The total amount in Euro dollars: 0.00
    The total amount in Canadian dollars: 0.00
    The total amount in Japanese Yen: 0.00
    The total amount in British Pounds: 0.00
    The total amount in Davidian dollars: 0.00

  11. #11
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Of course it will be all 0 because your function return nothing but "0"
    Code:
    return 0; /* indicates successful termination */
    the prototype for convert should be
    Code:
    /* function prototypes */
    double convert(int money,int * somearray)
    then write to that array instead of writing to all those local unallocated pointers.
    Last edited by nimitzhunter; 01-30-2011 at 11:04 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    Ok....I modified the prototype....but now getting compiler errors again.

    currency1.c: In function `main':
    currency1.c:39: too few arguments to function `convert'
    currency1.c:40: warning: passing arg 1 of `convert' makes integer from pointer without a cast
    currency1.c:40: too few arguments to function `convert'


    Code:
    #include <stdio.h>
    #include <time.h>
    #define SIZE 5
    
    /* function prototypes */
    double convert(int money, int * xchange);
    
    int main()
    {
       /* variable and array declarations */
       int money;
       srand( time(NULL));
       
       float xchange[] = { 1.00, 1.3593, 1.0013, 83.1300, 1.5837, rand(time)%100 };
          
    	/*
    	 * As of January 30, 2011, the current exchange values from www.google.com/finance.
    	 * USD (United States Dollar) = 1.00
    	 * EUR (Euro Dollar) = 1.3593
    	 * CAD (Canadian Dollar) = 1.0013
    	 * JPY (Japanese Yen) = 83.1300
    	 * GBP (Great Britian Pound) = 1.5837
    	 * DDS (Davidian Dollar Standard) = 3.56
    	 */
    
       /* prompt the user to enter a US dollar amount */
       printf( "Please enter the number of US dollars to be converted:\n" );
       scanf("%d.2f", money);
    
    	convert(money); /* function call */
    	convert(xchange); /* function call */
    
    //return 0; /* indicates successfull termination */
    } /* end main function */
    
    /* function prototypes */
    double convert(int money, int * xchange)
    
    {
    
       int *Ptr0, *Ptr1, *Ptr2, *Ptr3, *Ptr4, *Ptr5;
       int USD, EUR, CAD, JPY, GBP, DDS;
       Ptr0 = &USD;
       Ptr1 = &EUR;
       Ptr2 = &CAD;
       Ptr3 = &JPY;
       Ptr4 = &GBP;
       Ptr5 = &DDS;
    
       /* print the converted foreign currencies to the screen */
       *Ptr0 = 1.00 * money; /* USD */
       printf( "The total amount in US dollars: %8.2f\n", xchange);
    
       *Ptr1 = 1.3593 * money; /* EUR */
       printf( "The total amount in Euro dollars: %8.2f\n", xchange);
    
       *Ptr2 = 1.0013 * money; /* CAD */
       printf( "The total amount in Canadian dollars: %8.2f\n", xchange);
    
       *Ptr3 = 83.1300 * money; /* JPY */
       printf( "The total amount in Japanese Yen: %8.2f\n", xchange);
    
       *Ptr4 = 1.5837 * money; /* GBP */
       printf( "The total amount in British Pounds: %8.2f\n", xchange);
    
       *Ptr5 = rand(time)%100 * money; /* DDS */
       printf( "The total amount in Davidian dollars: %8.2f\n", xchange);
    
    return 0; /* indicates successful termination */
    } /* end function money */

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    Please help......!!!

  14. #14
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Code:
    int main()
    {
    ...
    // money has to be type float. 
    convert(money,xchange); /* function call */
    ...
    }
    that function doesn't have to return
    Code:
    void convert(float money, const int * xchange)
    {
    
       /* print the converted foreign currencies to the screen */
       printf( "The total amount in US dollars: %8.2f\n", xchange[0]*money);
       // you do the rest of them
    } /* end function money */
    "All that we see or seem
    Is but a dream within a dream." - Poe

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    Thanks to all who helped me on this project.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Class function pointers
    By VirtualAce in forum C++ Programming
    Replies: 40
    Last Post: 02-17-2005, 12:55 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM