Thread: Take a look at this program

  1. #1
    I'm less than sure.... abyssphobia's Avatar
    Join Date
    Aug 2004
    Posts
    112

    Take a look at this program

    Well a friend sent me this code and ask me if I can figure out what is wrong, but the proble is I donno C, I barely understood the instructions, its totally different to C++, well if someone knows Ill be glad.
    Code:
    #include<stdio.h> 
    #include<conio.h> 
    #include<string.h> 
    
    void main() 
    { 
    	float tmp,c,k,f;
    	int grd;
    	printf("\n\r How grade you want?\r 1.-Celcius \n\r 2.-Farenheit \n\r 3.-Kelvin");
    	scanf("\n\r %i",&grd);
    	scanf("\n\r %f",&tmp);
    	if (grd==1)
    	{
    		f=(tmp*1.8)+32;
    		k= tmp+273;
    		printf("\n\r Farenheit%f Kelvin%f",&f,&k);
    	}
    	if (grd==2)
    	{
    		c= (tmp-32)/1.8;
    		k= c+273;
    	}
    	if (grd==3)
    	{
    		c= tmp-273;
    		f= (c*1.8)+32;
    
    	}
    	
    
    	}
    Last edited by abyssphobia; 08-31-2004 at 12:50 AM.
    Have I crossed the line?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    void main()
    Code:
    scanf("\n\r %i",&grd);
    scanf("\n\r %f",&tmp);
    Fix those three lines and finish off main() [put a return 0; and a closing }] and go from there.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    A couple more things

    Code:
    #include<stdio.h> 
    #include<conio.h>  /* This is not a standard C header - you don't even need it here */
    #include<string.h>
    
    void main()
    { 
    	float tmp,c,k,f;
    	int grd;
    	printf("\n\r How grade you want?\r 1.-Celcius \n\r 2.-Farenheit \n\r 3.-Kelvin"); /* the escape sequence 
    '\n' includes the carriage return, so you only need the '\n'*/
    	scanf("\n\r %i",&grd); 
    	scanf("\n\r %f",&tmp);   /* Also, you don't need the '\n' with scanf */
    	if (grd==1)
    	{
    		f=(tmp*1.8)+32;
    		k= tmp+273;
    		printf("\n\r Farenheit%f Kelvin%f",&f,&k);  /* You specify a float but pass printf() an address */
    	}
    	if (grd==2)
    	{
    		c= (tmp-32)/1.8;
    		k= c+273;
    	}
    	if (grd==3)
    	{
    		c= tmp-273;
    		f= (c*1.8)+32;
    
    	}
    	
    
    	}
    Also, like Thantos said, its int main(void), not void main(). main() returns an int, therefore its wrong to declare main as returning nothing. It does not matter what your book or your teacher says. If they tell you void main() is correct, then they are wrong. Get a new teacher/book.

    When you are using float variables, try putting an f after the number.

    Code:
    c= tmp-273.0f;
    The '.0' is a matter of style - some say it makes the intention clearer..

    ~/

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Here's one way of doing what I think you're trying to do:
    Code:
    itsme@dreams:~/C$ cat temperature.c
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    enum { TEMP_C = 1, TEMP_F = 2, TEMP_K = 3 };
    
    // To avoid writing 6 functions, convert everything to kelvin first
    double deg_to_kelvin(double temp, int scale)
    {
      double rv;
    
      switch(scale)
      {
        case TEMP_C: rv = temp+273;          break;
        case TEMP_F: rv = (temp-32)/1.8+273; break;
        case TEMP_K: rv = temp;              break;
        default:
          puts("Unhandled scale!");
          exit(EXIT_FAILURE);
      }
    
      return rv;
    }
    
    double deg_to_celcius(double temp)
    {
      return temp-273;
    }
    
    double deg_to_farenheit(double temp)
    {
      return (temp-273)*1.8+32;
    }
    
    int main(void)
    {
      char buf[4096];
      int scale;
      double temp, kelvin_temp;
    
      do
      {
        printf("Temperature is in:\n  1) Celcius\n  2) Farenheit\n  3) Kelvin\n? ");
        fflush(stdout);
        fgets(buf, sizeof(buf), stdin);
        scale = atoi(buf);
      } while(scale < 1 || scale > 3);
    
      printf("And the temperature is: ");
      fflush(stdout);
      fgets(buf, sizeof(buf), stdin);
      temp = atof(buf);
    
      kelvin_temp = deg_to_kelvin(temp, scale);
      printf("\nCelcius  : %.2f\n", deg_to_celcius(kelvin_temp));
      printf("Farenheit: %.2f\n", deg_to_farenheit(kelvin_temp));
      printf("Kelvin   : %.2f\n", kelvin_temp);
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  5. #5
    I'm less than sure.... abyssphobia's Avatar
    Join Date
    Aug 2004
    Posts
    112
    thank you for all your help!

    ------------------------abyssphobia--------------------
    Have I crossed the line?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM