Thread: Problem with calling functions

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    28

    Problem with calling functions

    I made this program that is supposed to calculate conversions and pass the data back. Although it compiles, when I run it, it spews random incorrect data for the converted values. Please help.

    Code:
    #include <stdio.h>
        
    void getInput(int* fahrenheit, int* pounds, int* feet);
    void calc(int fahrenheit, int pounds, int feet, double* celsius, double*
              meters, double* kilograms);
    double calcTemp(int fahrenheit, double* celsius);
    double calcDistance(int feet, double* meters);
    double calcWeight(int pounds, double* kilograms);
    void display(int fahrenheit, int pounds, int feet, double celsius,
                 double kilograms, double meters);
       
    int main(void)
    {
       int fahrenheit, pounds, feet;
       double celsius, kilograms, meters;
    
    
     
          getInput(&fahrenheit, &pounds, &feet);
          calc(fahrenheit, pounds, feet, &celsius, &kilograms, &meters);
          display(fahrenheit, pounds, feet, celsius, kilograms, meters);
    
    
       return 0;
    }
     
    void getInput(int* fahrenheit, int* pounds, int* feet)
    {
        printf("\nPlease enter temeperature in Fahrenheit (integer): ");
        scanf("%d", &fahrenheit);
        printf("\nPlease enter a distance in feet (integer): ");
        scanf("%d", &feet);
        printf("\nPlease enter a weight in pounds (integer): ");
        scanf("%d", &pounds);
    }
       
    void calc(int fahrenheit, int pounds, int feet, double* celsius, double*
              meters, double* kilograms)
    {
       calcTemp(fahrenheit, celsius);
       calcDistance(feet, meters);
       calcWeight(pounds, kilograms);
    }
    
    
    double calcTemp(int fahrenheit, double* celsius)
    {
       *celsius = (fahrenheit -32) * (float)5/9;
    }
    
    
    double calcDistance(int feet, double* meters)
    {
       *meters = (float)feet * 0.3048;
    }
    
    
    double calcWeight(int pounds, double* kilograms)
    {
     *kilograms = (float)pounds * 0.45359237;
    }
        
    void display(int fahrenheit, int pounds, int feet, double celsius, double
                 kilograms, double meters)
    {
       printf("\n%15s%15s%15s%15s", "Original", "Value", "Converted to", "Value");
       printf("\n%15s%15s%15s%15s", "-----------", "-----", "------------", "-----"$
       printf("\n%15s%15d%15s%15f", "Fahrenheit", fahrenheit, "Celsius", celsius);
       printf("\n%15s%15d%15s%15f", "Feet", feet, "Meters", meters);
       printf("\n%15s%15d%15s%15f", "Pounds", pounds, "Kilograms", kilograms);
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The fahrenheit, pounds, feet are already pointers you do NOT need to get the address of them to use with scanf.
    Tim S.
    Code:
    void getInput(int* fahrenheit, int* pounds, int* feet)
    {
        printf("\nPlease enter temeperature in Fahrenheit (integer): ");
        scanf("%d", &fahrenheit);
        printf("\nPlease enter a distance in feet (integer): ");
        scanf("%d", &feet);
        printf("\nPlease enter a weight in pounds (integer): ");
        scanf("%d", &pounds);
    }
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Also your three conversion functions should be type void because they do not return anything.
    Code:
    void calcTemp(int fahrenheit, double* celsius); 
    void calcDistance(int feet, double* meters); 
    void calcWeight(int pounds, double* kilograms);

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    28
    Thanks so much, that fixed my initial problem! Now however I'm trying to add another conversion, from celsius to kelvin. When I try to compile I am getting the error, "invalid operands to binary +". Any help is much appreciated, I am new to coding and it is a little frustrating.

    Code:
    #include <stdio.h>
    
    void getInput(int* fahrenheit, int* pounds, int* feet);
    void calc(int fahrenheit, int pounds, int feet, double* celsius, double*
              meters, double* kilograms, double* kelvin);
    double calcTemp(int fahrenheit, double* celsius, double* kelvin);
    double calcDistance(int feet, double* meters);
    double calcWeight(int pounds, double* kilograms);
    void display(int fahrenheit, int pounds, int feet, double celsius,
                 double kilograms, double meters, kelvin);
    
    
    int main(void)
    {
       int fahrenheit, pounds, feet;
       double celsius, kilograms, meters, kelvin;
    
    
    
    
          getInput(&fahrenheit, &pounds, &feet);
          calc(fahrenheit, pounds, feet, &celsius, &kilograms, &meters,
               &kelvin);
          display(fahrenheit, pounds, feet, celsius, kilograms, meters,
                  kelvin);
    
    
       return 0;
    }
    
    
    void getInput(int* fahrenheit, int* pounds, int* feet)
    {
        printf("\nPlease enter temeperature in Fahrenheit (integer): ");
        scanf("%d", fahrenheit);
        printf("\nPlease enter a distance in feet (integer): ");
        scanf("%d", feet);
        printf("\nPlease enter a weight in pounds (integer): ");
        scanf("%d", pounds);
    }
    
    
    void calc(int fahrenheit, int pounds, int feet, double* celsius, double*
              meters, double* kilograms, double* kelvin)
    {
       calcTemp(fahrenheit, celsius, kelvin);
       calcDistance(feet, meters);
       calcWeight(pounds, kilograms);
    }
    
    
    double calcTemp(int fahrenheit, double* celsius, double* kelvin)
    {
       *celsius = (fahrenheit -32) * (float)5/9;
       *kelvin = celsius + 273.15;
    }
    
    
    double calcDistance(int feet, double* meters)
    {
       *meters = (float)feet * 0.3048;
    }
    double calcWeight(int pounds, double* kilograms)
    {
       *kilograms = (float)pounds * 0.45359237;
    }
        
    void display(int fahrenheit, int pounds, int feet, double celsius, double
                 kilograms, double meters, double kelvin)
    {
       printf("\n%15s%15s%15s%15s%15s%15s", "Original", "Value", 
              "Converted to", "Value", "Converted to", "Value");
       printf("\n%15s%15s%15s%15s%15s%15s", "-----------", "-----",
              "------------", "-----", "------------", "-----" );
       printf("\n%15s%15d%15s%15f", "Fahrenheit", fahrenheit, "Celsius",
              celsius, "Kelvin", kelvin);
       printf("\n%15s%15d%15s%15f", "Feet", feet, "Meters", meters);
       printf("\n%15s%15d%15s%15f", "Pounds", pounds, "Kilograms", kilograms);
       printf("\n\n");
    }

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Note how you access celsius in the line previous.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    28
    Quote Originally Posted by rags_to_riches View Post
    Note how you access celsius in the line previous.
    I see that, but I don't see how that is causing a problem. Like I said I am very new to programming.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Getting a decent compiler, and really turning up the warning level will find a lot of newbie mistakes before you even get to run the code.

    Code:
    $ gcc -W -Wall -Wextra bar.c
    bar.c:10: error: expected declaration specifiers or ‘...’ before ‘kelvin’
    bar.c: In function ‘main’:
    bar.c:25: error: too many arguments to function ‘display’
    bar.c: In function ‘calcTemp’:
    bar.c:55: error: invalid operands to binary + (have ‘double *’ and ‘double’)
    bar.c: At top level:
    bar.c:68: error: conflicting types for ‘display’
    bar.c:9: note: previous declaration of ‘display’ was here
    bar.c: In function ‘display’:
    bar.c:76: warning: too many arguments for format
    It might not seem like it to you at the moment, but getting something to compile in C is exceedingly easy.
    The real trick comes later, when you try and make it do what you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You forgot asterisk: *kelvin = *celsius + 273.15;
    Also you ignored my help regarding void function type and prototype.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    28
    Quote Originally Posted by nonoob View Post
    You forgot asterisk: *kelvin = *celsius + 273.15;
    Also you ignored my help regarding void function type and prototype.

    Thank you, that seems to have fixed the error, and I didn't ignore your advice, for whatever reason my teacher specified that they should be int and double.

    I am now wondering how I would align the values so that the decimal places are lined up.


    Ex.)

    300.052
    112.444
    145.676
    Last edited by SaraDawkins; 02-16-2012 at 02:55 PM.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    28
    Here is my code so far. I have two problems left that I can't figure out:
    1. The meters and kilograms values are being swapped when they are displayed.
    2. I can't figure out how to align decimals in my display chart.


    Code:
    #include <stdio.h>
    
    void getInput(int* fahrenheit, int* pounds, int* feet);
    void calc(int fahrenheit, int pounds, int feet, double* celsius, double*
              meters, double* kilograms, double* kelvin, int* inches);
    double calcTemp(int fahrenheit, double* celsius, double* kelvin);
    double calcDistance(int feet, double* meters, int* inches);
    double calcWeight(int pounds, double* kilograms);
    void display(int fahrenheit, int pounds, int feet, double celsius,
                 double kilograms, double meters, double kelvin, int inches);
    
    
    int main(void)
    {
       int fahrenheit, pounds, feet, inches;
       double celsius, kilograms, meters, kelvin;
    
    
    
    
          getInput(&fahrenheit, &pounds, &feet);
          calc(fahrenheit, pounds, feet, &celsius, &kilograms, &meters,
               &kelvin, &inches);
          display(fahrenheit, pounds, feet, celsius, kilograms, meters,
                  kelvin, inches);
    
    
       return 0;
    }
    
    
    void getInput(int* fahrenheit, int* pounds, int* feet)
    {
        printf("\nPlease enter temeperature in Fahrenheit (integer): ");
        scanf("%d", fahrenheit);
        printf("\nPlease enter a distance in feet (integer): ");
        scanf("%d", feet);
        printf("\nPlease enter a weight in pounds (integer): ");
        scanf("%d", pounds);
    }
    
    
    void calc(int fahrenheit, int pounds, int feet, double* celsius, double*
              meters, double* kilograms, double* kelvin, int* inches)
    {
       calcTemp(fahrenheit, celsius, kelvin);
       calcDistance(feet, meters, inches);
       calcWeight(pounds, kilograms);
    }
    
    
    double calcTemp(int fahrenheit, double* celsius, double* kelvin)
    {
       *celsius = (fahrenheit -32) * (float)5/9;
       *kelvin = *celsius + 273.15;
    }
    
    
    double calcDistance(int feet, double* meters, int* inches)
    {
       *meters = (float)feet * 0.3048;
       *inches = feet * 12;
    }
        
    double calcWeight(int pounds, double* kilograms)
    {
    *kilograms = (float)pounds * 0.45359237;
    }
        
    void display(int fahrenheit, int pounds, int feet, double celsius, double
                 kilograms, double meters, double kelvin, int inches)
    {
       printf("\n%13s%9s%14s%9s%14s%14s", "Original", "Value",
              "Converted to", "Value", "Converted to", "Value");
       printf("\n%13s%9s%14s%9s%14s%14s", "--------", "-----",
              "------------", "-----", "------------", "-----" );
       printf("\n%13s%9d%14s%9.3f%14s%9.3f", "Fahrenheit", fahrenheit,
              "Celsius",celsius, "Kelvin", kelvin);
       printf("\n%13s%9d%14s%9.3f%14s%9d", "Feet", feet, "Meters", meters,
              "Inches", inches);
       printf("\n%13s%9d%14s%9.3f", "Pounds", pounds, "Kilograms",
              kilograms);
       printf("\n\n");
    }

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    void calc(int fahrenheit, int pounds, int feet, double* celsius, double*
              meters, double* kilograms, double* kelvin, int* inches);
    ////
          calc(fahrenheit, pounds, feet, &celsius, &kilograms, &meters,
               &kelvin, &inches);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling functions
    By cda67 in forum C Programming
    Replies: 2
    Last Post: 10-14-2011, 11:56 PM
  2. calling functions
    By njasmine1 in forum C Programming
    Replies: 2
    Last Post: 12-29-2010, 10:28 AM
  3. not calling functions
    By BungleSpice in forum C Programming
    Replies: 9
    Last Post: 03-06-2004, 01:36 AM
  4. Calling functions help
    By ForlornOdium in forum C++ Programming
    Replies: 14
    Last Post: 09-29-2003, 08:40 PM
  5. I need some help on calling other functions in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 12:07 PM