Question

While using Ch Standard Interpreter, getchar() only runs every other line.

C:/> char a = getchar();
C:/> char b = getchar();
b
C:/> char c = getchar();
C:/> char d = getchar();
d

I have the same issue when using scanf("%c", &a) instead; in Vim the statement is skipped.

printf("\nType of Something\nA for SomethingA\nB for SomethingB "
        "\nC for SomethingC\n\nSelect (A,B,C) > ");
char letter = getchar(); // This statement gets skipped
return 0;
Was it helpful?

Solution

When you type "A" on the keyboard, you first press "A", and then you press "ENTER", right? So getchar() or scanf("%c", ....) gets two characters to read: "A", and the newline character from the "ENTER" key.

If you use your code in a loop, or just repeatedly, the first getchar() will read the newline character from the previous input.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top