This happens because you type a letter and then you press enter. Use another getchar()
to eat the trailing newline.
So change this:
k = toupper(getchar());
to this:
k = toupper(getchar());
getchar(); // eat the trailing newline
When the user inputs something, it goes to the stdin (standard input) stream and the system makes sure to store what the user typed in a internal buffer. So here is what happened with your code:
So the solution is to eat the trailing newline!
Easter eggs tips:
You should receive this:
warning: implicit declaration of function ‘printf’
because you lack of the IO header, thus you should add in the top of your main file this:
#include <stdio.h>
Similarly you should add:
#include <ctype.h> // for toupper()
#include <stdlib.h> // for exit()
Another solution would be to use fgets(), see this question for more C - scanf() vs gets() vs fgets().
I had a similar issue to yours with scanf()
and I was in your shoes, so I had written down the solution at the time.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…