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?

3
  • 3
    You mention scanf repeatedly, but your code only uses getchar. Please use more care in your questions to avoid confusing answerers. Commented Aug 5, 2013 at 6: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(). Commented Aug 6, 2013 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 Commented Feb 13, 2016 at 9:21

2 Answers 2

2

You are not handling newlines.

See this answer for clarification.

Sign up to request clarification or add additional context in comments.

Comments

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) ;

3 Comments

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. (For example, if the user enters 'aaa\n'.)
@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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.