Thread: Help

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    12

    Angry Help

    Im trying to get this program to work, it stops at

    else


    WHat have i done wrong here, i want to be able to specifiy whether fahrenheit or celsius can be used.

    Regards

    #include <stdio.h>


    int main()

    {
    float fahrenheit, celsius;
    int a;

    printf("Enter a value of 1 for Celsius, 2 for Fahrenheit:\n");
    scanf("%d", &a);


    if (a == 1)

    printf("Enter a degrees in Celcius:\n");
    scanf("%f",& celsius);
    fahrenheit = 1.8*celsius + 32.0;
    printf("Fahrenheit = %f\n", fahrenheit);

    else
    printf("Enter a degrees in Fahrenheit:\n");
    scanf("%f", & fahrenheit);
    celsius = (fahrenheit - 32) * 5 / 9;
    printf("Celsius = %f\n", celsius);

    return 0;


    }

  2. #2
    Registered User geqo's Avatar
    Join Date
    Jan 2003
    Posts
    2
    err, you need to use code tags, learn c and use brackets:

    Code:
    #include <stdio.h>
    
    int main() { 
        float fahrenheit, celsius;
        int a;
    
        printf("Enter a value of 1 for Celsius, 2 for Fahrenheit:\n");
        scanf("%d", &a);
    
        if (a == 1) {
            printf("Enter a degrees in Celcius:\n"); 
            scanf("%f",& celsius);
            fahrenheit = 1.8*celsius + 32.0;
            printf("Fahrenheit = %f\n", fahrenheit);
        } else {
            printf("Enter a degrees in Fahrenheit:\n");
            scanf("%f", & fahrenheit);
            celsius = (fahrenheit - 32) * 5 / 9;
            printf("Celsius = %f\n", celsius);
        }
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    12

    THANx

    thanks, ive copyed your text it works, im trying to update mine, still compiling error, ill try and fix it properly..

    thanks!!!!

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Just to let you see how vulnerable your code is.

    If someone types for int a; "hello" then according to your code the app will skip the Celcius part and go directly to the fahrenheit part....
    So look up these functions: isalpha and isdigit.
    This way some "dumb" user wont say that your program doesnt do what its supposed to do.

  5. #5
    ********
    Guest
    i'd do it like this...............

    Code:
    #include <stdio.h>
    
    void Celcius (void);
    void Fahrenheit (void);
    
    float fahrenheit, celsius;
    
    int main() {
        
        int a;
    
        clrscr();      /*clears the screen*/
        
        printf("\nEnter a value of 1 for Celsius, 2 for Fahrenheit: ");
        a=getch(); /*alternative to scanf, gets entry from keyboard*/
    
       switch(a){
      case '1' : Celcius();
                     break;
      case '2': Fahrenheit();
    	break;
      default:  printf("\n Please select a valid option");
    	 break;
       }
    
      getch();
      return 0;
    
    }
    
    void Celcius (void){
    
    
    	printf("\nEnter a degrees in Celcius: ");
    	scanf("%f",& celsius);
    
    	fahrenheit = 1.8*celsius + 32.0;
    	printf("Fahrenheit = %f", fahrenheit);
    
    }
    
    void Fahrenheit (void)
    {
    	printf("\nEnter a degrees in Fahrenheit:");
    	scanf("%f", & fahrenheit);
    	celsius = (fahrenheit - 32) * 5 / 9;
    	printf("Celsius = %f", celsius);
    }
    .......but thats me anyway

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Neither getch() or clrscr() are ANSI.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >int main()
    int main ( void ) is more correct.

    >clrscr(); /*clears the screen*/
    Are you sure? Without a definition, this function could do anything. Why not include conio.h and ensure that this line works. Also note that anything in conio.h is nonstandard and may not exist on your implementation.

    >a=getch(); /*alternative to scanf, gets entry from keyboard*/
    getch is also defined in conio.h. The comment is also misleading, scanf is considerably more complicated and powerful than getch, so getch would really be an alternative to
    Code:
    scanf ( "%c", &a );
    Another thing to note is that stdin isn't always the keyboard.

    >scanf("%f",& celsius);
    Always check the return value of functions that accept outside input. The chances of getting invalid data are huge.

    >scanf("%f", & fahrenheit);
    Ditto

    >float fahrenheit, celsius;
    You really should avoid global variables when you can. If you must use them, try to give them file scope only by declaring them as static.

    >int a;
    A more descriptive name is in order.
    Code:
    #include <stdio.h>
    
    static double Celcius ( void );
    static double Fahrenheit ( void );
    
    int main ( void )
    {
      int option;
      
      printf("1) Celsius\n2) Fahrenheit\nChoose an option: ");
      option = getchar();
      
      switch ( option ) {
      case '1':
        printf ( "Celsius = %f\n", Celcius() ); 
        break;
      case '2':
        printf ( "Fahrenheit = %f\n", Fahrenheit() );
        break;
      default:
        printf("Invalid option\n");
      }
      
      (void)getchar();
      return 0;
    }
    
    double Celcius ( void )
    {
      double celsius;
    
      printf ( "Enter a degrees in Celcius: " );
    
      if ( scanf ( "%lf", &celsius ) != 1 )
        return 0.0;
      
      return 1.8 * celsius + 32.0;
    }
    
    double Fahrenheit ( void )
    {
      double fahrenheit;
    
      printf ( "Enter a degrees in Fahrenheit:" );
    
      if ( scanf ( "%lf", &fahrenheit ) != 1 )
        return 0.0;
    
      return ( fahrenheit - 32.0 ) * ( 5.0 / 9.0 );
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed