### Thread: casting problem i believe

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

Join Date
Sep 2003
Posts
53
Rep Power
15

#### casting problem i believe

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

[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. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

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

Join Date
Sep 2003
Posts
53
Rep Power
15
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.
4. No Profile Picture
Offensive Member
Devshed Novice (500 - 999 posts)

Join Date
Oct 2002
Location
in the perfect world
Posts
622
Rep Power
31
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){