-5
#include <stdio.h>

int main() {

    char choice;
    float x,y;

    start:
    printf("[c] Converts Celsius -> Fahrenheit\n[f] Converts Fahrenheit -> Celsius\n\n\n");
    printf("Enter Choice: ");
    scanf("%c",&choice);
    if (choice!='c' || choice!='f' || choice!='x') {
            printf("Wrong Choice: Try Again!");
            goto start;

        } if (choice!=x)
          printf("Input Value: ");
          scanf("%f",&x);
                if (choice =='c')
                    y = 1.8 * x + 32
                else
                    y = (x-32) * (5/9)
          printf("Result: %.2f",y);
          exit:

    return 0;
}

My instructor posted this but when I tried it, it have errors, need help to fix it.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52

2 Answers2

2

There are quite some things wrong with this code:

  1. Using goto instead of a while loop is not the way to make this.
  2. Your second if (if (choice != x)) is missing braces. This way only the first statement is executed if true. The rest is always executed.
  3. You are missing semicolons ; after your calculations.
  4. Your boolean logic in the first if is incorrect.
  5. In your second if you are comparing against a variable, instead of a fixed value.
  6. Your Fahrenheit-to-Celsius arithmetic is incorrect.

Hope these tips help you. I won't post the correct code, that's up to you as you are still learning ;-).

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
0

First of all you should forget that there is the goto statement in C.

All programs that contained a goto statement and I saw including some IBM's source code had numerous bugs related to goto statements.

The goto statement makes programs difficult to read and more difficult to maintain and modify them.

Using the goto statement is the same as traffic violatins.:)

Instead of this code snippet

start:
printf("[c] Converts Celsius -> Fahrenheit\n[f] Converts Fahrenheit -> Celsius\n\n\n");
printf("Enter Choice: ");
scanf("%c",&choice);
if (choice!='c' || choice!='f' || choice!='x') {
        printf("Wrong Choice: Try Again!");
        goto start;

    }

that at least has an invalid condition in the if statement

if (choice!='c' || choice!='f' || choice!='x') 

instead of valid

if (choice!='c' && choice!='f' && choice!='x') 

You could write for example

enum { Celsius = 'c', Fahrenheit = 'f', Exit = 'x' };
char choice;
int valid_input;

do
{
    printf( "[c] Converts Celsius -> Fahrenheit\n"
            "[f] Converts Fahrenheit -> Celsius\n"
            "[x] Exit\n\n" );

    printf( "Enter Choice: " );

    if ( scanf( "%c ", &choice ) != 1 ) choice = Exit;

    valid_input = choice == Celsius || choice == Fahrenheit || choice == Exit;

    if ( !valid_input ) puts( "Wrong Choice: Try Again!\n" );
} while ( !valid_input );

Your program contains numerous bugs. Insetad of to list them I will show how the program could be written

#include <stdio.h>

int main( void )
{    
    enum { Celsius = 'c', Fahrenheit = 'f', Exit = 'x' };
    char choice;
    int valid_input;

    do
    {
        printf( "[c] Converts Celsius -> Fahrenheit\n"
                "[f] Converts Fahrenheit -> Celsius\n"
                "[x] Exit\n\n" );

        printf( "Enter Choice: " );

        if ( scanf( "%c ", &choice ) != 1 ) choice = Exit;

        valid_input = choice == Celsius || choice == Fahrenheit || choice == Exit;

        if ( !valid_input ) puts( "Wrong Choice: Try Again!\n" );
    } while ( !valid_input );


    switch( choice )
    {
        float x, y;

        case Celsius: case Fahrenheit:
            printf( "Input Value: " );
            if ( scanf( "%f", &x ) == 1 )
            {                
                if ( choice == Celsius )
                    y = 1.8 * x + 32;
                else
                    y = ( x - 32 ) * ( 5.0f / 9.0f );
                printf( "Result: %.2f\n", y );
            }                
        case Exit:
            puts( "Bye!" );
            break;
    }

    return 0;
}            

if to enter sequentially

c 20 x

then the output will look like

[c] Converts Celsius -> Fahrenheit
[f] Converts Fahrenheit -> Celsius
[x] Exit

Enter Choice: c
Input Value: 20
Result: 68.00
Bye!
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335