0

I´m trying to make a program that tells you if a letter is a vowel o consonant and then ask the user if he/she wants to use the program again.

Using just the switch statement works totally fine. My problem comes when inserting the do-while loop. On the first try, the program works smoothly, but in the second and following loops, after asking the user if he wants to try again by typing 1, the program "jumps" the part where the user writes its input (scanf("%c", &letter)) and executes the rest of the program considering the previously typed 1 as the input and messing up everything.

I´ve tried looking for the answer on similar questions and videos, but I´m quite new and I just can´t grasp it. I have another program with a similar problem. I will really appreciate your help :)

int main()
{
  char letter;
  int new_try;
  do {
    printf("Is you letter a vocal or a consonant?\n");
    printf ("\nPlease submit a letter:\n");
    scanf("%c", &letter);

    switch(letter) {
      case 'a':
      case 'A':
      case 'e':
      case 'E':
      case 'i':
      case 'I':
      case 'o':
      case 'O':
      case 'u':
      case 'U': 
        printf("This letter is a vowel\n");
        break;
      default : printf("This letter is a consonant\n");
        break;
    }

    printf("Do you wish to try again?\n [1.Yes    2. No]\n");
    scanf("%d", &new_try);
  }
  while(new_try != 2);
  return 0;
}
Jens
  • 8,423
  • 9
  • 58
  • 78
  • 2
    `scanf` with the `"%c"` conversion specifier takes the `'\n'` left from `scanf` with `"%d"` specifier at the end of the loop. Use `" %c"` instead. (note the `space`) The `"%c"` and `"%[...]"` specifiers do NOT consume leading whitespace on their own -- the remaining conversion specifiers do. Always ***validate the return*** of every input function used. – David C. Rankin Aug 09 '19 at 23:51

1 Answers1

0

You need to clean the input buffer. Otherwise, as long as there are characters in your input buffer, the program will not wait for new input from user. Here is the solution.

#include <stdio.h>
int main() {

    char letter;
    int new_try;
    do{

    printf("Is you letter a vocal or a consonant?\n");
    printf ("\nPlease submit a letter:\n");
    scanf("%c", &letter);
    while ((getchar()) != '\n');

    switch(letter) {

    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U': printf("This letter is a vowel\n");
    break;
    default : printf("This letter is a consonant\n");
    break;
    }

    printf("Do you wish to try again?\n [1.Yes    2. No]\n");
    scanf("%d", &new_try);
    }while(new_try != 2);


return 0;
}

Update

As @David C. Rankin mentioned, using scanf(" %c", &letter); can also addresss your issue. However, it doesn't address a matching failure. In the above case, you also need to validate the input.

MTMD
  • 1,162
  • 2
  • 11
  • 23
  • 1
    `scanf(" %c", &letter);` will do. But that will not handle a *matching failure*. Your advice to clear the input buffer is correct and will handle that circumstance as well. (you need to emphasize that unless the **return** from `scanf` if validated -- there is no way to know what you are dealing with) – David C. Rankin Aug 09 '19 at 23:54
  • @DavidC.Rankin Fwiw, you cause me intrigue as to what possible conditions, short of stream error or eof state, a *matching failure* can happen against `%c`. =) – WhozCraig Aug 10 '19 at 00:00
  • @WhozCraig You are correct with `"%c"` -- you won't have a matching failure -- I was talking about the `"%d"` at the end of the loop leaving the remnants of the failed integer conversion in the input buffer `:)` – David C. Rankin Aug 10 '19 at 00:02
  • @DavidC.Rankin That makes a ton more sense. Thanks for the clarification. – WhozCraig Aug 10 '19 at 00:03
  • Even ignoring matching failures, this code doesn't work. The `"%d"` at the bottom of the loop will leave a `\n` behind, which the `"%c"` at the top of the loop will read. – user3386109 Aug 10 '19 at 00:06