0

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?

This isn't my real name
  • 4,869
  • 3
  • 17
  • 30
LinhSaysHi
  • 642
  • 5
  • 14
  • 30
  • 3
    You mention scanf repeatedly, but your code only uses `getchar`. Please use more care in your questions to avoid confusing answerers. – Chris Hayes Aug 05 '13 at 06:59
  • @ChrisHayes SO allows you to edit other people's questions. Since, as you point out, the code uses only `getchar()` for input, I have edited the question to replace the mentioned of `scanf()` with `getchar()`. – This isn't my real name Aug 06 '13 at 21:08
  • The return type of `getchar` is `int` and you should store it into a variable of type `int` and compare with `EOF`; See also [what happens if you use `char c = getchar()` instead of `int`](http://stackoverflow.com/questions/35356322/difference-between-int-and-char-in-getchar-and-putchar) – Antti Haapala -- Слава Україні Feb 13 '16 at 09:21

2 Answers2

2

getchar() leaves a newline character which is consumed in the next iteration. Add another getchar() to ignore the newline:

c = getchar ();
getchar(); // To consume the newline

Or read out all the until newline:

int c=0;
while ((c = getchar()) != '\n' && c != EOF) ;
P.P
  • 117,907
  • 20
  • 175
  • 238
  • This is correct, although somewhat dangerous if the next character is not a newline. If you want to preserve non-newline characters you can use [ungetc](http://stackoverflow.com/questions/13993742/is-there-anyway-to-peek-at-the-stdin-buffer). (For example, if the user enters 'aaa\n'.) – Chris Hayes Aug 05 '13 at 07:01
  • @ChrisHayes ungetc() is guaranteed to push back only character. So that's not safe either. getchar(), scanf() are all prone errors in one way or other. I'd suggect to fgets() + sscanf(). But that seems to be an overkill for this. – P.P Aug 05 '13 at 07:07
  • 1
    I don't understand what you mean. ungetc is perfectly safe in this context, because you should only need to push back characters, one at a time. That is, pop off a character with `getchar`, and if it's not a newline, use `ungetc` and continue your loop. – Chris Hayes Aug 05 '13 at 07:09
2

You are not handling newlines.

See this answer for clarification.

Community
  • 1
  • 1
Henrik
  • 4,254
  • 15
  • 28