Thread: Caculation Problem

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    43

    Caculation Problem

    Hello

    I'm currently working on a program that when you input a number in Kelvin, it will give u the temperature in either Farenheit or Celcius then tell you what state its in (liquid, solid, gas)

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <ctype.h>
    
    double readnumber(void)
    {
    	char ch[10];
    	double input;
    	printf("\n");
    	printf("Enter a temperature in Kelvin:\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("Enter 'F' for Fahrenheit or 'C' for Celcius:\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 is the temperature program!");
            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);
    }
    atm though, it doesn't matter what temperature it is, it will always be a solid and then always end up being the same answer, every time when I go to repeat the loop

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First, are you getting good input in readnumber(), specifically in the variable named input?

    Second, are you getting good returns from your functions, one by one?

    All you need is a print statement at the end of each function to find out which function is bad.

    Troubleshooting a program like yours is not hard, and it's a skill you should be practicing.

    I don't see errors in the code, but it's late. My guess is that atof() in readnumber(), is getting goofed up by the newline or end of string char that fgets() has put into your char string. Just a guess though.

    If so, just use a small for loop from strlen(ch) to 0, (decrementing the index each time through the loop), and remove them.

    That's a nice program, but don't let your troubleshooting skills lay dormant. You'll need them, as long as you program.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    There are a couple of things that I think are very wrong with this program.

    1 is that if we look at your function signatures, they're written entirely wrong. They're supposed to have a return type, name, and parameter list, with types and names for each variable.
    Code:
    double centigrade (double input)
    {
       double converted = 0.0;
       converted = ( input - 32.0 ) / 1.8;
       return converted;
    }
    The next issues are pointed out by the body of the function I wrote for you. First, be consistent with the type of the expression you write. In the example, the expression's result is a double result. This practice avoids unexpected type conversions in your statements. Lastly, look at the return statement in the function. Keep in mind that a function can only return one variable, if you need a function to return more than one thing, consider making the callee pass in a pointer as an argument.

    The other things your functions do, like test for the state of matter, are done pretty poorly (substances and elements by and large have different boiling points and freezing points). Nevertheless, you can find the state of a substance out using any temperature unit. Don't view it as a waste of time to write one function that does that with just one unit. You can always convert to the expected unit just before you output the answer.
    Last edited by whiteflags; 10-12-2009 at 12:46 AM.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    ok!

    thanks for help guys! Its starting to work a lot better now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM