Hey.

The task. Translate inches to cm. I enter 2i, should bring 5.08 cm.

#include <stdio.h> int main() { int x; char ch; ch = getchar(); scanf("%d%c", &x, &ch); //ch = getchar(); if (x == 2 && ch == 'i') printf("%lf cm\n", 2.54 * x); //2i = 5.08cm } 

Where is the mistake? I may not understand something, the check seems to be worth it.

  • Slightly clarify the wording of the answer @dzhioev. The result of getchar () was overwritten in the variable ch 'i', which was entered into scanf (). If you entered 2ii then everything would work. - avp

1 answer 1

Why do you call getchar? After all, it reads the first character from the input, so the next you get is not what you expected.

  • in-and, this, too, - skegg
  • one
    removed the getchar () function. Why did I use it?) So it was char ch)) thanks for the answer) - marioxxx