2

I've been writing a simple program to check if input letter is a vowel, and my code doesn't work. The program should take characters as input one by one until % is entered, which will make it exit. It checks if input chars are vowels, and prints the result. Also it reports an error if input is not a letter. The problem is, it breaks out of the loop on the second step. Thank you for help, in advance. PS Sorry, didn't write that there's no error message, it just breaks out of the loop.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
  char processed='q';
  while(processed != '%')
  {
    printf("Enter letter to check if it's a vowel, %% to quit.\n");
    char input = getchar();
    processed = tolower(input);
    printf("%c\n", processed);
    if (processed == '%')
      break;
    if (processed < 'a' || processed > 'z')
    {
      fprintf(stderr, "Input should be a letter\n");
      exit(1);  
    }
    switch(processed)
    {
      case 'a':
      case 'e':
      case 'i':
      case 'o':
      case 'u':
      case 'y':
        printf ("Vowel\n");
        break;
      default:
        printf ("Non-vowel\n");
    }
  }
  exit(0);
}
1
  • 3
    When you type "with an error message", the very next thing you should type is the exact error message. We can't see your screen or read your mind from here, and since you already know what the error is you could do the favor of providing it. It makes it much easier to solve your problem if you give us all the information to use to do so, and making it easier for us gets you answers faster. Commented Mar 28, 2012 at 3:00

2 Answers 2

5

Presumably you're entering a character and then hitting [ENTER]. So, in actuality you are entering two characters -- the letter you typed and a line feed (\n). The second time through the loop you get the line feed and find that it's not a letter, so you hit the error case. Perhaps you want to add something like:

if (processed == '\n') {
    continue;
}
Sign up to request clarification or add additional context in comments.

3 Comments

getchar() doesn't wait for a newline to return, so why would they hit enter?
getchar() is equivalent to fgetc(stdin). therefore after you hit [some-char] + [enter], enter be read next from stdin on the second iteration... pubs.opengroup.org/onlinepubs/007904875/functions/fgetc.html
@Inafziger: If standard input is line-buffered, which is often the default if it is attached to a terminal, then getchar() will appear to wait for a newline.
3

Someone else mentioned that you're hitting enter after each letter of input, and thus sending a newline ('\n') into your program. Since your program doesn't have a case to handle that, it isn't working right.

You could add code to handle the newline, but using scanf would be easier. Specifically, if you replaced

char indent = getchar();

with

char indent;
scanf("%c\n", &indent);

scanf() would handle the newline and just return back the letters you're interested in.

And you should check scanf()'s return value for errors, of course.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.