Thread: Need help with a temperature c prog, thanks

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    28

    Need help with a temperature c prog, thanks

    Hi,

    Im trying to make a program that gathers a kelvin temp, changes it to either celcius/fahrenheight then determines the state of a sample of water (gas, solid, liquid)
    When I wrote this program out the first time i couldnt get it to work, so i kept on altering it and altering it and now i just cant even think :/ can anyone look over my code and help me straighten out a few things? it says there is 1 error and some warnings but I know there is lots wrong

    thanks.

    Code:
     
    /*Prac7prog1*/
    
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <ctype.h>
    
    double readnumber()
    {
    	char ch[10];
    	double input;
    	printf("\n");
    	printf("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°");
    	printf("°°°°°°°	   Enter a temperature in Kelvin:    °°°°°°°");
    	printf("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°");
    	fflush(stdout);
    	fgets(ch,sizeof ch,stdin);
    	input=atof(ch);
    	return(input);
    }
    
    int temptype()
    {
    	char ch[10];
    	char option;
    	do {
    	printf(" ****** Select Which option do you wish view ****** ");
    	printf("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°");
    	printf("°°  Enter 'F' for Fahrenheit or 'C' for Celicus:  °°");
    	printf("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°");
    	option=getch();
    	option=tolower(option);
    	}while(!((option == 'f')||(option == 'c')));
    	scanf("%c\n",option);
    	return(option);
    }
    
    double fahrenheit(double inpF,char option)
    {
            if (option == 'f')
            {
    	   double inpF
    	   inpF=((9*(input-273))/5)+32;
    	   if (inpF>=212) type = 1;
    	   else if (inpF>=33 && inpF<=211) type = 2;
               else if (inpF<=32) type = 3;
    	   return(inpF);
    	}
    }
    
    double celcius(double inpC,char option)
    {
            if (option == 'c')
            {
    	   double inpC
    	   inpC=input-273;
    	   if (inpC>=101) type = 1;
    	   else if (inpC>=1 && inpC <= 100) type = 2;
               else if (inpC<=0) type = 3;
    	   return(inpC);
    	}
    }
    
    int Main(void)
    {
    	double input=0.0, type;
    	char option, opt;
    
            printf("This program checks to see whether a temperature is in Gas, Liquid or Solid state");
            do
    	{
    	     printf("\n\nDo you wish to continue? (y/n): ");
    	     	do
    	        {
    	     	opt = getch();
    	        opt = tolower (opt);
    	        }while (!((opt=='y')||(opt=='n')));
    	        printf("%c\n",opt);
                    if (opt=='y')
    	
                    input = readnumber();
    	        option = temptype();
    
    
             	switch (type)
    		{
    		  case 1:
    			 printf("\nThe state of the water is: Gas, at %.0f degrees Celsius.\n", input);
    			 break;
    		  case 2:
    			 printf("\nThe state of the water is: Liquid, at %.0f degrees Celsius.\n", input);
    			 break;
    		  case 3:
    			 printf("\nThe state of the water is: Solid, at %.0f degrees Celsius.\n", input);
    			 break;
    	          case 4:
    	                 printf("\nThe state of the water is: Gas, at %.2f degrees Fahrenheit.\n", input);
    	                 break;
    	          case 5:
    	                 printf("\nThe state of the water is: Liquid, at %.2f degrees Fahrenheit.\n", input);
    	                 break;
    	          case 6:
    	                 printf("\nThe state of the water is: Solid, at %.2f degrees Fahrenheit.\n", input);
    	                 break;
    		}
    	}
    	while (opt!='n');
    	return(0);
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    scanf("%c\n",option);
    in function temptype() is not needed
    On your menus you might want to put a \n at the end of the printf() string to output in a better format.
    In function fahrenheit() you are trying to assign type a value but it is not a variable in scope. Same with function celcius().

    You need curley brackets after if (opt=='y') so that the code only gets exectucted if they choose y

    Also where are you calling the fahrenheit() and celcius() functions from?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    it says there is 1 error and some warnings but I know there is lots wrong
    How about actually posting your errors some time. Just because you don't understand them, doesn't mean they're not helpful.

    Code:
    int Main(void)
    I think you mean "main".

    Also, why are you bothering to check the option type in each function? Check the option, and based on whatever it is, call the correct function. There's no need to pass the option type to the calculation functions. You already know how to use a switch, so use one for your option types, where each case calls the correct function.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    28

    thanks for responses :)

    Thantos when you said
    "In function fahrenheit() you are trying to assign type a value but it is not a variable in scope. Same with function celcius()."

    what do you actually mean by not a variable in scope?
    because 'type' variable is in main function does that mean i have to pass it to the fahrenheight and celcius functions in order for them to use it?

    i've changed a few things in the code and now i got 5 errors and 4 warnings most of which seem to be in celcius/fahrenheight and in calling some of my functions :/

    Error, line 45 Declaration syntax error in function fahrenheit
    Error, line 46 Undefined symbol 'type' in function fahrenhieght
    Warning, line 51 Parameter 'inpC' is never used in function celcius
    Error, line 57 Declaration syntax error in function celcius
    Error, line 58 Undefined symbol 'type' in function celcius
    Warning, line 63 Parameter 'inpC' is never used in function celcius
    Warning, line 88 call to function 'readnumber' with no prototype in function main
    Warning, line 89 Call to function 'temptype' with no prototype in function main
    Error, line 98 Switch selection expression must be of integral type in function main

    heres the code now:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <ctype.h>
    
    double readnumber(void)
    {
    	char ch[10];
    	double input;
    	printf("\n");
    	printf("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°\n");
    	printf("°°°°°°°	   Enter a temperature in Kelvin:    °°°°°°°\n");
    	printf("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°\n");
    	fflush(stdout);
    	fgets(ch,sizeof ch,stdin);
    	input=atof(ch);
    	return(input);
    }
    
    int temptype(void)
    {
    	char ch[10];
    	char option;
    	do {
    	printf(" ****** Select Which option do you wish view ****** ");
    	printf("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°\n");
    	printf("°°  Enter 'F' for Fahrenheit or 'C' for Celicus:  °°\n");
    	printf("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°\n");
    	option=getch();
    	option=tolower(option);
    	}while(!((option == 'f')||(option == 'c')));
    	return(option);
    }
    
    double fahrenheit(double inpF)
    {
            {
    	   double inpF
    	   inpF=((9*(input-273))/5)+32;
    	   if (inpF>=212) type = 4;
    	   else if (inpF>=33 && inpF<=211) type = 5;
               else if (inpF<=32) type = 6;
    	   return(type);
    	}
    }
    
    double celcius(double inpC)
    {
            {
    	   double inpC
    	   inpC=input-273;
    	   if (inpC>=101) type = 1;
    	   else if (inpC>=1 && inpC <= 100) type = 2;
               else if (inpC<=0) type = 3;
    	   return(type);
    	}
    }
    
    int main(void)
    {
    	double input=0.0, type;
    	char option, opt;
    
            printf("This program checks to see whether a water sample is in Gas, Liquid or Solid state");
            do
    	{
    	     printf("\n\nDo you wish to continue? (y/n): ");
    	     	do
    	        {
    	     	opt = getch();
    	        opt = tolower (opt);
    	        }while (!((opt=='y')||(opt=='n')));
    	        printf("%c\n",opt);
                    if (opt=='y')
    		{
                       input = readnumber();
                       option = temptype();
                       if(option == 'c')
                       {
                            celcius(input);
                       }
                       else if(option == 'f')
                       {
                            fahrenheit(input);
                       }
                            switch (type)
    			{
    		  	case 1:
    				 printf("\nThe state of the water is: Gas, at %.0f degrees Celsius.\n", input);
    				 break;
    		  	case 2:
    				 printf("\nThe state of the water is: Liquid, at %.0f degrees Celsius.\n", input);
    				 break;
    		 	case 3:
    				 printf("\nThe state of the water is: Solid, at %.0f degrees Celsius.\n", input);
    				 break;
    	          	case 4:
    	                 	 printf("\nThe state of the water is: Gas, at %.2f degrees Fahrenheit.\n", input);
    	                 	 break;
    	          	case 5:
    	                 	 printf("\nThe state of the water is: Liquid, at %.2f degrees Fahrenheit.\n", input);
    	                 	 break;
    	          	case 6:
    	                 	 printf("\nThe state of the water is: Solid, at %.2f degrees Fahrenheit.\n", input);
    	                 	 break;
    			}
    		}
    	}
    	while (opt!='n');
    	return(0);
    }
    Once again, thanks for taking the time to answer my posts

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    double fahrenheit(double inpF)
    {
            {
    	   double inpF
    1) You're missing a ; at the end of the last line.
    2) You've got a redefinition of the same variable. The variable in the function prototype has the same name as the one you're declaring.
    3) While not wrong, you have an unneeded extra { pair in this function.

    The same thing applies for your celcius function.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >double fahrenheit(double inpF)

    You are returning an int, so this should be:
    int fahrenheit(double inpF)

    You also need to declare type in this function:
    int type;

    >double celcius(double inpC)
    Same here.

    > double input=0.0, type;
    type is not a double:
    int type;

    > celcius(input);
    This function returns the type, so:
    type = celcius(input);

    > fahrenheit(input);
    Same here:
    type = fahrenheit(input);

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    what do you actually mean by not a variable in scope?
    because 'type' variable is in main function does that mean i have to pass it to the fahrenheight and celcius functions in order for them to use it?
    Exactly. A variable local to one function is not accessiable by another function.
    Thats why you are getting the following errors
    Error, line 58 Undefined symbol 'type' in function celcius
    Error, line 46 Undefined symbol 'type' in function fahrenhieght

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    28
    thanks! you guys helped heaps its actually running now
    but not very well hehe i still got 3 warnings that i cant solve

    Warning, line 93 call to function 'celcius' with no prototype in function main
    Warning, line 97 call to function 'fahrenheit' with no prototype in function main
    (and i still not calling these right?)

    Warning, line 74 'input' is assigned a value that is never used in function man
    (im not passing input right either am i )

    Also when i do actually run the program, at the end it displays the original inputed 'kelvin' temperature instead of the new celcius or fahrenheit ones, and the result is always solid
    I just realised that putting 'input' in the switch function was wrong because after it get checked in fahrenheit and celcius it does not return (only has original value anyway). Does this mean i have to find some way to return inpC and inpF to the switch as well as type?

    sorry to post the code again, but i've changed a few things
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <ctype.h>
    
    double readnumber(void)
    {
    	char ch[10];
    	double input;
    	printf("\n");
    	printf("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°\n");
    	printf("°°	   Enter a temperature in Kelvin:         °°\n");
    	printf("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°\n");
    	printf("\n ");
    	fflush(stdout);
    	fgets(ch,sizeof ch,stdin);
    	input=atof(ch);
    	return(input);
    }
    
    int temptype(void)
    {
    	char ch[10];
    	char option;
    	do {
    	printf("\n");
            printf("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°\n");
    	printf("°°  Enter 'F' for Fahrenheit or 'C' for Celcius:  °°\n");
    	printf("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°\n");
    	option=getch();
    	option=tolower(option);
    	}while(!((option == 'f')||(option == 'c')));
    	return(option);
    }
    
    int fahrenheit(input)
    {
    
    	double inpF;
    	int type;
    	inpF=((9*(input-273))/5)+32;
    	if (inpF>=212) type = 4;
    	else if (inpF>=33 && inpF<=211) type = 5;
            else if (inpF<=32) type = 6;
    	return(type);
    }
    
    int celcius(input)
    {
    	double inpC;
    	int type;
    	inpC=input-273;
    	if (inpC>=101) type = 1;
    	else if (inpC>=1 && inpC <= 100) type = 2;
            else if (inpC<=0) type = 3;
    	return(type);
    }
    
    int main(void)
    {           
            int type;
    	double input=0.0;
    	char option, opt;
    
            printf("This program checks to see whether a water sample is in Gas, Liquid or Solid state");
            do
    	{
    	     printf("\n\nDo you wish to continue? (y/n): ");
    	     	do
    	        {
    	     	opt = getch();
    	        opt = tolower (opt);
    	        }while (!((opt=='y')||(opt=='n')));
    	        printf("%c\n",opt);
                    if (opt=='y')
    		{
                       input = readnumber();
                       option = temptype();
                       if(option == 'c')
                       {
                            type = celcius(input);
                       }
                       else if(option == 'f')
                       {
                            type = fahrenheit(input);
                       }
                            switch (type)
    			{
    		  	case 1:
    				 printf("\nThe state of the water is: Gas, at %.2f degrees Celcius.\n", input);
    				 break;
    		  	case 2:
    				 printf("\nThe state of the water is: Liquid, at %.2f degrees Celcius.\n", input);
    				 break;
    		 	case 3:
    				 printf("\nThe state of the water is: Solid, at %.2f degrees Celcius.\n", input);
    				 break;
    	          	case 4:
    	                 	 printf("\nThe state of the water is: Gas, at %.2f degrees Fahrenheit.\n", input);
    	                 	 break;
    	          	case 5:
    	                 	 printf("\nThe state of the water is: Liquid, at %.2f degrees Fahrenheit.\n", input);
    	                 	 break;
    	          	case 6:
    	                 	 printf("\nThe state of the water is: Solid, at %.2f degrees Fahrenheit.\n", input);
    	                 	 break;
    			}
    		}
    	}
    	while (opt!='n');
    	return(0);
    }
    -thanks

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You are missing the type for the parameters for celcius() and fahrenheit().

    It should be
    Code:
    (double input)
    Do this: Change your calls to fahrenheit() and celcius() to
    Code:
    (&input)
    and change the parameter list to
    Code:
    (double *input)
    And change the following lines inside the functions

    Code:
    /*In fahrenheit*/
    inpF=((9*(input-273))/5)+32;
    /* change to */
    inpF=((9*(*input-273))/5)+32;
    
    /*In celcius*/
    inpC=input-273;
    /* change to */
    inpC=*input-273;
    and right before the returns add in:
    Code:
    /*In fahrenheit*/
    *input = inpF;
    /*In celcius*/
    *input = inpC;
    What I'm having you do is pass the address of the variable that holds the input and in the function we are modifying the actual variable through the use of a pointer.

    This is a tactic you have to use when you want to have a function affect more then one variable in the caller's scope.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    28
    it works! no errors or warnings
    thanks so much for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Temperature Conversion code problems
    By eroth88 in forum C Programming
    Replies: 6
    Last Post: 10-22-2006, 01:24 AM
  2. Motherboard Temperature
    By MK4554 in forum Windows Programming
    Replies: 1
    Last Post: 07-18-2006, 10:51 AM
  3. Temperature conversion...
    By Onslaught in forum C Programming
    Replies: 3
    Last Post: 10-21-2005, 01:15 PM
  4. read the CPU temperature
    By BianConiglio in forum Windows Programming
    Replies: 2
    Last Post: 05-19-2004, 11:41 AM
  5. Howdy! Temperature prog
    By [BIO]Asgard in forum C++ Programming
    Replies: 5
    Last Post: 02-26-2004, 06:45 PM