We have to write a program to convert a user entered Kelvin temperature to either celsius or fahrenheit and also display the state of the water. I found the same question on here and used it to fix some problems I was having, but I still can't compile and I don't know what I'm doing wrong.

/* Program to convert temperature and display state */

#include <stdio.h>

/* Get user input */
void getTemp (int kelvin)
{
	printf("Welcome to the temperature conversion program.\n");
	printf("Please enter a the sample temperature in Degrees Kelvin: \n");
	scanf("%d%*c", &kelvin);
	
	return ();
}

/* Convert temperature */
void getType (char type)
{
	int kelvin, celsius, fahrenheit;
	
	printf("Do you wish to convert the temperature to (c) for Celsius, or (f) for Fahrenheit? \n");
	scanf("%c%*c", &type);
	
	switch(type)
	{
		case 'c':
		celsius = convC(kelvin, celsius);
		break;
		case 'f':
		fahrenheit = convF (fahrenheit, celsius, kelvin);
		break;
		
		default:
		printf("You have entered an invalid option");
		break;
	}
	
	return ();
}

void convC (int kelvin, int celsius)
{
	(celsius = (kelvin - 273));
	
	if(celsius > 99)
		{
			printf("The water is in a gas state at %d degrees celsius.\n", celsius);
		}
	if(celsius > 0 && celsius < 100)
		{
			printf("The water is in a liquid state at %d degrees celsius.\n", celsius);
		}
	if(celsius < 1)
		{
			printf("The water is in a solid state at %d degrees celsius.\n", celsius);
		}
		
		return();
}

void convF (int kelvin, int celsius, int fahrenheit)
{
	(celsius = (kelvin - 273));
	(fahrenheit = (9 * celsius / 5) + 32));
	
	if(fahrenheit > 210.2)
		{
			printf("The water is in a gas state at %d degrees fahrenheit.\n", fahrenheit);
		}
	if(fahrenheit > 32 && fahrenheit < 212)
		{
			printf("The water is in a liquid state at %d degrees fahrenheit.\n", fahrenheit);
		}
	if(fahrenheit < 33)
		{
			printf("The water is in a solid state at %d degrees fahrenheit.\n", fahrenheit);
		}
		
		return();
}

int main ()
{
	int kelvin;
	char type;
	
	getTemp(kelvin);
	getType(type);
	
	return (0);
}

Neither do we. If it won't compile, it probably has errors. If it has errors, the compiler tells you what they are and where. Since we are not compilers, we need details.

These are the errors I get:
Error E2188 tempy.cpp 12: Expression syntax in function getTemp(int)
Error E2467 tempy.cpp 12: 'getTemp(int)' cannot return a value in function getTe
mp(int)
Error E2268 tempy.cpp 26: Call to undefined function 'convC' in function getType
(char)
Error E2268 tempy.cpp 29: Call to undefined function 'convF' in function getType
(char)
Error E2188 tempy.cpp 37: Expression syntax in function getType(char)
Error E2467 tempy.cpp 37: 'getType(char)' cannot return a value in function getT
ype(char)
Error E2188 tempy.cpp 57: Expression syntax in function convC(int,int)
Error E2467 tempy.cpp 57: 'convC(int,int)' cannot return a value in function con
vC(int,int)
Error E2379 tempy.cpp 63: Statement missing ; in function convF(int,int,int)
Error E2188 tempy.cpp 78: Expression syntax in function convF(int,int,int)
Error E2467 tempy.cpp 78: 'convF(int,int,int)' cannot return a value in function
convF(int,int,int)

What are those () after the return statements? I know you didn't learn that from your book.

You need to add prototypes for your functions at the top of the code.

Don't ask -- look it up in your index.

@mastertoilet :
>> When you declare a method's return type as void, it doesn't return any value.
>> Learn more about function prototype and parameter passing in C.

what's wrong:
1 -

return ();

this is not C valid. Do:

return;

Or write nothing at all, you don't need to call return at the end of a void function.
2 - lines like:

(celsius = (kelvin - 273));

You don't need to put parenthesis, this can be written as:

celsius = kelvin - 273;

Put parenthesis in things like: a = (b+c) * d; or around operations if you don't know which one will be "executed" first (it's "operator precedence" rules of C).
Some of those lines have a wrong number of parenthesis too. Check.
3 - and this is a big one.
You write:

getTemp(kelvin);

I understand what you want to do. But you do it wrong. It works just by luck.
You pass kelvin by value, ie. the compiler copies its value on the stack and pass it to getTemp. Normally whatever getTemp does it won't affect the value of kelvin in the function main. You take the address in sscanf and by luck it changes the value of kelvin in the main function.
You should do:

void getTemp(int *kelvin)
{
  scanf("%d", kelvin);
}

and

getTemp(&kelvin);

This is passing parameter by address, not by value.
And then you should pass kelvin to getType, which should be:

void getType(int kelvin)

and type be a local variable to getType.
You hit a fundamental notion. It's the by value/by address parameter passing of the C language. Surely this is explained in any C tutorial.

Edited 5 Years Ago by BarSed: n/a

here's a simple program for you without function so that you can understand how it works....

#include<stdio.h>
#include<conio.h>
main()
{ int a,b,c;
printf("Welcome to temparature convertor");
printf("Enter Temperature in kelvin");
scanf("%d",&c);
printf("\n1.Convert to celcius\n2.Convert to fahrenheit");
scanf("%d",&a);
switch(a)
{
case 1:
b=c-273;
printf("Temperature in celcius is %d",b);
break;
case 2:
b=((c*9)/5)-460;
printf("Temperature in fahrenheit is %d",b);
break;
default:
printf("You selected wrong choice");
break;
}

getch();
return 0;
}

PS-If ur problem is solved mark the thread as solved

Comments
PS If you're going to post code, format it, don't use non standard function, and use proper syntax.

3 - and this is a big one.
You write:

getTemp(kelvin);

I understand what you want to do. But you do it wrong. It works just by luck.
You pass kelvin by value, ie. the compiler copies its value on the stack and pass it to getTemp. Normally whatever getTemp does it won't affect the value of kelvin in the function main. You take the address in sscanf and by luck it changes the value of kelvin in the main function.
You should do:

void getTemp(int *kelvin)
{
  scanf("%d", kelvin);
}

and

getTemp(&kelvin);

This is passing parameter by address, not by value.

No you shouldn't. For a function that passes back a single value, you should return the value. You should not overwrite the value passed in.

int getTemp()
{
  int temp;
  scanf("%d", &temp);
  return temp;
}

WaltP-First i would like to say that i used all proper standards and syntax and moreover he hasn't told that he want to do it using function and so i thought to give him simpler solution so that he can understand and if necessary program acoordingly....
I hate this....for no reason you dislike my post.....

WaltP-First i would like to say that i used all proper standards and syntax

Yes, but that doesn't mean you should use the pass by reference to return a single value. You should return that value as a return value.

and moreover he hasn't told that he want to do it using function and so i thought to give him simpler solution so that he can understand and if necessary program acoordingly....

void getTemp (int kelvin) was in his code, so he did want to use a function.

I hate this....for no reason you dislike my post.....

We don't want people to post answers to problems the OP needs to figure out himself. Especially when the code is badly formatted and doesn't even work. Try running your code and convert to F. It fails.

Edited 5 Years Ago by WaltP: n/a

but it looks like that only.....he told that he saw this code in this forum so he used it....So i just showed him easy way to do so......
I think so it's better not to post further in this area....People are not cooperative and are full of hatred!!!

Edited 5 Years Ago by tomato.pgn: n/a

Comments
Save the drama for the theatre.

I guess if you feel disagreement is hatred, fine.

To me, disagreement is a learning opportunity. If you ask "why do you disagree" and try to understand the answer, you may learn something new. You can also explain why you disagree and maybe I can learn something.

But if disagreement means "I hate you" then there's not much I can do.

And I don't know you well enough to hate you. And you haven't said anything for me to hate.

It's better if you read this....

We have to write a program to convert a user entered Kelvin temperature to either celsius or fahrenheit and also display the state of the water. I found the same question on here and used it to fix some problems I was having, but I still can't compile and I don't know what I'm doing wrong.

He found a solution and he wanted to use it so he posted this code....what i feel i must post him for his basics i posted.....
He hasn't mentioned that he want to do with functions only....
If so i will post help in functions if he desires....

I've made a mistake. I was not responding to the correct post. I therefore have edited my response above. You probably still won't like it. and you didn't help him with functions at all, so why are you complaining?

And I still don't hate you.

Edited 5 Years Ago by WaltP: n/a

you must use this code to solve your problem......

#include <stdio.h>
#include<conio.h>
 int getType ()
{
	int kelvin, celsius, fahrenheit;
	char type;
	printf("\n");
	printf("Enter the temperature in kelvin and In what u want to convert to (Press c 

for celcius and f for fahrenheit)");
	scanf("%d %c",&kelvin,&type);
	switch(type)
	{
		case 'c':
		celsius = convC(kelvin);
		break;
		case 'f':
		fahrenheit = convF (kelvin);
		break;
		default:
		printf("You have entered an invalid option");
		break;
	}

	return 0;
}

int convC (int kelvin)
{
        int celsius;
	(celsius = (kelvin - 273));
	printf("\n");
	if(celsius > 99)
		{
			printf("The water is in a gas state at %d degrees celsius.\n", 

celsius);
		}
	if(celsius > 0 && celsius < 100)
		{
			printf("The water is in a liquid state at %d degrees celsius.\n", 

celsius);
		}
	if(celsius < 1)
		{
			printf("The water is in a solid state at %d degrees celsius.\n", 

celsius);
		}

		return 0;
}

int convF ( int kelvin)
{
         int fahrenheit,celsius;
	(celsius = (kelvin - 273));
	(fahrenheit = (9 * celsius / 5) + 32);
	printf("\n");
	if(fahrenheit > 210.2)
		{
			printf("The water is in a gas state at %d degrees fahrenheit.\n", 

fahrenheit);
		}
	if(fahrenheit > 32 && fahrenheit < 212)
		{
			printf("The water is in a liquid state at %d degrees fahrenheit.\n", 

fahrenheit);
		}
	if(fahrenheit < 33)
		{
			printf("The water is in a solid state at %d degrees fahrenheit.\n", 

fahrenheit);
		}

		return 0;
}

int main ()
{       int kelvin;
	char type;
	getType();
	getch();
	return (0);
}

There were many errors in your code....
like...

return ();

It must be return 0;

void convF (int kelvin, int celsius, int fahrenheit)

here the parameters passed are reversed i.e.,while calling you use this syntax...

convF (fahrenheit, celsius, kelvin);

Hope your problem is solved...

@WaltP:-

I've made a mistake. I was not responding to the correct post. I therefore have edited my response above. You probably still won't like it. and you didn't help him with functions at all, so why are you complaining?

Now happy...i told him how to work with function....

PS-If your problem is solved then mark the thread as solved...

This article has been dead for over six months. Start a new discussion instead.