0

Can anyone please explain what am i doing wrong here? It seems whatever is under if is not being executed. but i don't understand why.

#include<stdio.h>

main()
{   float Fah, Cel, Temp;
    char  scale,F,C;
    printf("Press F to convert to fahrenheit\n");
    printf("Press C to convert in celcius\n");
    scanf("%c",&scale);

    if(scale==C)
    { 
      printf("Enter the temp\n");

      scanf("%f",&Fah);  
      Temp=(Fah-32)/1.8;
    }
    else
    {
     if(scale==F)
     {
       printf("Enter the temp\n");

       scanf("%f",&Cel);
       Temp=(Cel*1.8)+32;
     }
    } 
    printf("The temperature  is %f\n",Temp);
    getch();
}

Output:

Press F to convert to fahrenheit
Press C to convert in celcius
C
The temperature  is 0.000000
Press any key to continue . . .
adi99
  • 73
  • 3

1 Answers1

4

Your code has several issues:

1.

Your approach is incorrect.

You need to use if (scale == 'C') instead of if (scale == C) and if (scale == 'f') instead of if (scale == F) and omit the definition of C and F as char variables.

'C' is a character constant.

C is, in your case, a uninitialized char variable. It does not contain any specified value. Thus, the behavior of the program is undefined.

Same goes for F.


2.

You should have an else statement after if (scale == 'C') and else if (scale == 'F') which covers the case when the input provided is different than 'C' or 'F' (you will never know what the user may input).


3.

Furthermore, since scanf() leaves a trailing newline character back in stdin - Made by the press to Enter - you need to catch this newline character by f.e. getchar() following each scanf() statement to not skip the following input operation.


4.

main() is not correct. Use at least int main(), or even better int main (void) to be standard-compliant.


5.

getch() isn´t part of stdio.h and even the C standard library in general. It is either a function from conio.h (Windows/MS-DOS) or the ncurses-library (Linux). Thus, you need to include the specific header.

Probably it was a typo and you meant getchar() instead, but worth to hint.


Side note:

You should always check the return value of scanf() if the input was successfullly consumed.


Result:

#include <stdio.h>

int main (void)
{   
    float Fah, Cel, Temp;
    char scale;
    printf("Press F to convert to fahrenheit\n");
    printf("Press C to convert in celcius\n");
    scanf("%c", &scale);
    getchar();

    if (scale == 'C')   
    { 
        printf("Enter the temp\n");

        if (scanf("%f", &Fah) != 1)
        {
             fputs("Wrong Input!", stderr);
             return 1;
        } 
        getchar();                  // For catching newline character.
 
        Temp = (Fah-32) / 1.8;
    }

    else if (scale == 'F')
    {
        printf("Enter the temp\n");

        if (scanf("%f", &Cel) != 1)
        {
            fputs("Wrong Input!", stderr);
             return 1;
        } 
        getchar();                   // For catching newline character.

        Temp = (Cel * 1.8) + 32;
    }

        else 
        {
               // Case for different input than 'F' and 'C'.
               // Probably Error routine.
               printf("Wrong input!\n");
        }

    printf("The temperature  is %f\n", Temp);
    getchar();

    return 0;
}
Community
  • 1
  • 1
  • Thank You for such a descriptive answer. I'm trying to grasp everything you've pointed out. So, my first question is- I've declared the variable ***scale*** to have ***char*** data type. And i thought ***scanf*** should hold the character ***C*** or ***F*** as typed by the user as i have defined it as such. Then why is it necessary to use *** if (scale == 'C')*** instead of just ***if (scale == C)*** – adi99 May 30 '20 at 13:51
  • @adi99 `C`, as you used it, is a variable of type `char`. This `C` is just a name for a variable, but no character *value* itself. To proof if `scale` contains the *value* of the character `C` we need to compare it to a proper character (value), not a variable. To confuse you even more imagine the following: `char C = 'C'`;` - This defines C as a `char` variable and assigns the actual value of the character C to the variable called C. In this way you could even use `if (scale == C)` as the variable C holds now the *value* of the C character. – RobertS supports Monica Cellio May 30 '20 at 14:10
  • @adi99 - But we don´t need to use a variable called C to hold the C character value here. We can just compare `scale` by the actual character value itself. For doing so, we need to surround the C by `'` -> `'C'` to make it a so called "*character constant*". – RobertS supports Monica Cellio May 30 '20 at 14:10
  • I apologize but i still have query. While I do get your point as to why declaring ***C*** and ***F*** as ***char*** is unnecessary but what I'm having trouble with is this part ```scanf("%c",&scale); if(scale=='C') ``` When i rewrite my code removing ***F*** and ***C*** from char making it ```float Fah, Cel, Temp; char scale;``` and keep the ***if*** section as ```if(scale==C)``` i'll get an error saying ***C*** is not declared. But it works when its written as ```if(scale=='C')```. Can u plz explain why – adi99 May 30 '20 at 14:46
  • . Besides in ur comment earlier u wrote ```char C = 'C'```; i'd like u to explain how is it different from ```char C = C```. – adi99 May 30 '20 at 14:46
  • @adi99 Pop quiz. - If you use `if (scale == C)` what do you think is `C` here? Re-Read my first comment. – RobertS supports Monica Cellio May 30 '20 at 15:05
  • @adi99 If you use `if (scale == C)`, `C` is a variable. If you remove the declaration of the variable `C`, the compiler will throw an error. – RobertS supports Monica Cellio May 30 '20 at 15:12
  • @adi99 Furthermore: What do you think is the right `C` here -> `char C = C`? Is it a character value or just the variable `C`? – RobertS supports Monica Cellio May 30 '20 at 15:17