-1

i created a code that converts the temperature into another form, like if you want to convert celcius to farenheit or kevlin and vice versa. But it isnt working as i thought.

code:-

#include <stdio.h>

int main()
{
  float a, b;
  char c, e;

  printf("What you want to change\ntype c for celcius, f for farenheit or k for kelvin: ");
  scanf("%c", &c);

  switch (c)
  {
  case 'c':
    printf("Now put the celcius value you want to convert: ");
    scanf("%f", &a);

    printf("Now in which you want to change\ntype f for farenheit or k for kelvin: ");
    scanf("%c", &e);

    switch (e)
    {
    case 'f':
      printf("The farenheit value of %0.2f degree celcius: %0.2f\n", a, a*1.8+32);
      break;

    case 'k':
      printf("The kelvin value of %0.2f degree celcius: %0.2f\n", a, a+273.15);
      break;       

    default:
      printf("You didnt put the correct operation");
      break;        

    }
    break;

  case 'f':
    printf("Now put the farenheit value you want to change into celcius: ");
    scanf("%f", &a);
    b = ((a-32)*5)/9;

    printf("Now in which you want to change\ntype c for celcius or k for kelvin: ");
    scanf("%c", &e);

    switch (e) {
  case 'c':
    printf("The celcius value of %0.2f degree farenheit: %.2f\n", a, b);
    break;

  case 'k':
    printf("The kelvin value of %0.2f degree farenheit: %0.2f\n", a, b+273.15);
    break;

  default:
    printf("You didnt put the correct operation");
    break;        
    }
    break;


  case 'k':
    printf("Now put the kelvin value you want to change: ");
    scanf("%f", &a);

    if (a>=0) {
      printf("Now in which you want to change\ntype c for celcius or f for farenheit: ");
      scanf("%c", &e);        
      b=(a-273.15)*1.8+32;
      switch (e) {
  case 'c':
    printf("The celcius value of %0.2f kelvin in celcius is: %0.2f\n", a, a-273.15);
    break;

  case 'f':
    printf("The farenheit value of %0.2f kelvin in farenheit is: %0.2f\n", a, b);
    break;

  default:
    printf("You didnt put the correct operation");
    break;                
      }}

    else {
      printf("Kelvin cannot be less than zero");
    }
    break;

  default:
    printf("You didnt put the correct spell of what you want to change\n");                
    break;
  }
  return 0;
}

I was trying to make it so that it will ask the user in which you want to change but it didnt worked as i thought. It isnt asking for what you want to change and skips that scanf function.

Mark Benningfield
  • 2,800
  • 9
  • 31
  • 31
  • what error are you getting? – Abhimanyu Sharma Nov 02 '22 at 05:03
  • 1
    Make all, e.g. `scanf(" %c", &c);` (note the additional space) The `"%c"` fomat-specifier does not discard leading whitespace meaning it will take the `'\n'` left over from the user pressing ENTER as your input. (`"%[ ]"` and `"%n"` are the other two specifiers that do NOT discard leading whitespace) – David C. Rankin Nov 02 '22 at 05:21

1 Answers1

1

The issue you're getting is that scanf leaves a newline character in the buffer after getting the float. Then the scanf for the character gets the newline and you're switch statement goes to the default statement. This is a major flaw of scanf and can be very confusing for new users of C. One potential solution to this is to clear the buffer using this:

while ((c = getchar()) != '\n' && c != EOF);

This uses getchar (which takes one character from the same buffer as scanf) and will loop until getchar gets a newline or gets to the end of the buffer. Then when you run scanf it will have an empty buffer an take input from the user.

mossx
  • 46
  • 2
  • Better to use `{ }` at the end rather than `';'` (or at least put the `';'` on the line below) for readability. May also explain that you are looping to empty the `'\n'` from `stdin` (or any other characters in the event of a *matching-failure* with the prior input) – David C. Rankin Nov 02 '22 at 05:23