Thread: Problem with function call

  1. #1
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43

    Problem with function call

    Hi there,

    I am trying to make a program to convert currency.

    I am stuck in a function call, more specifically this one part below. Instead of getting the the amount of euros I get for exchanging USD, I get a very long number which does not make sense. As I don't get any warning or error and all compiles fine I don't know what I am doing wrong... Can anyone help? / thnx, Bluetxxth.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define RATE 1.3 // the exchange rate
    
    
    /*Function takes input from the user (USD and it converts them to EUR
     * Param: doble - usd integer which holds the ammount of USD
     * Return: double - change in Euros
     */
    
    double usd_to_eur(double usd) {
    
        double rate = 1 / RATE; // euro conversion rate
        double change;
    
        printf("Enter currency in USD: ");
        scanf("%f", &usd);
    
        change = usd * rate;
    
        return change;
    }
    
    int main(void) {
    
        double chng, // variable which contains the return value of the function usd_to_eur()
                eur; //variable for the actual parameter int the function call which corresponds to the formal parameter
    
        chng = usd_to_eur(eur);
        printf("Exchange is: %.2f", chng); // prints the result of the currency conversion
    
    
        return (0);
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Compiling with errors on:
    Code:
    eur.c:17: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double *’

  3. #3
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43

    poblem with function call

    Hi there,

    Thank much !! ... I am using Net Beans on a Windows environment and I don't see that warning when I compile...

    br,

    Gabriel

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It doesn't make sense to be passing eur to the function usd_to_eur() since it's never assigned. Within the function it is used as a local variable usd which is fine... but it didn't need to be passed as a parameter.

  5. #5
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43

    persisting problem with a function call

    Hi and thank you all for the help.

    I have taken notice of the two suggestions I have received. and that part of the program is working fine now.
    Last edited by bluetxxth; 10-07-2010 at 09:05 AM.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    C uses pass-by-value when passing parameters to functions. You need to use pass-by-reference.

  7. #7
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43
    Hi there,

    Thank you for your answer... I guess you mean I need a pointer.. .if so I was suspecting this....

    BR,

    Bluetxxth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. how to get function call stack
    By George2 in forum C Programming
    Replies: 18
    Last Post: 11-11-2006, 07:51 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM