#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    53
    Rep Power
    11

    casting problem i believe


    I am using Gnut compiler and C. I am recieving the same out put everytime for (celsius_depth(temp);

    please help


    [code]
    #include<stdio.h>

    int celsius_depth (int);
    double celsius_to_fahren (int);

    int main(void) {
    int depth;
    int temp;


    /*begin the loop*/


    while (depth != -99999) {

    printf("\nEnter depth in km (-99999 to exit):");
    scanf("%d", &depth);

    if (depth !=-99999)
    printf("\nAt %d km below the earth's surface, the temperature is"
    "\n%d degrees Celsius or %d degrees Fahrenheit\n", depth,
    celsius_depth(depth),(int)celsius_to_fahren(celsius_depth(temp)));


    }


    return(0);
    }


    int celsius_depth (int depth) {
    int tempC;

    tempC = (10*depth)+20;

    return(tempC);
    }


    double celsius_to_fahren (celsius){
    double fahrenheit;
    int temp;

    fahrenheit = ((9.0/5.0)*celsius) + 32;

    return (fahrenheit);
    }
    Last edited by gman932; September 23rd, 2003 at 04:04 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    55
    Rep Power
    12
    don't cast to int, change your printf()'s formatting to %f's on dloating point/double value represenations.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    53
    Rep Power
    11
    I now get values of 0.000000 on both
    Code:
    printf("\nAt %d km below the earth's surface, the temperature is" 
    "\n%f degrees Celsius or %f degrees Fahrenheit\n", depth, 
    celsius_depth(depth),(int)celsius_to_fahren(celsius_depth(temp)));

    if i make them %f then i get 68.000 for all inputs then.....still no change.
    Last edited by gman932; September 23rd, 2003 at 07:20 PM.
  6. #4
  7. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    You never use or init the variable Temp until you send it into the conversion funtion. Therefore it will contain rubbish.

    A good compiler would have told you this.

    (int)celsius_to_fahren(celsius_depth(temp))

    should be

    (int)celsius_to_fahren(celsius_depth(depth))

    is this a typo

    double celsius_to_fahren (celsius){

    ie

    double celsius_to_fahren (int celsius){
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa

IMN logo majestic logo threadwatch logo seochat tools logo