I am creating a program that returns true if a character I input is an upper case letter and the loop stops when I input the char '0'. The code is as follows:
#include <stdio.h>
int main (void)
{
char c;
do
{
printf ("Please enter character to check if uppercase: ");
c = getchar ();
if ( (c >= 'A') && (c <= 'Z') )
{
printf ("true\n");
}
else
{
printf ("false\n");
}
} while ( c != '0');
return 0;
}
However, I get weird behavior (Output):
Please enter character to check if uppercase: a
false
Please enter character to check if uppercase: false
Please enter character to check if uppercase: b
false
Please enter character to check if uppercase: false
Please enter character to check if uppercase: A
true
Please enter character to check if uppercase: false
Please enter character to check if uppercase: 0
false
-
The "false" thats comes after the prompt is not what I typed.
For example:
- 1. Prompt appears
- 2. I type in the character 'a'
- 3. Console prints false
- 4. Prompt appears but also printed is the word 'false' next to prompt
- 5. Prompt appears again
So it seems that getchar() is taking input that is not coming from me. Any ideas?