Home > Mobile >  I need help writing a Loop that finds vowels in C
I need help writing a Loop that finds vowels in C

Time:02-04

I need to prompt the user for a letter. If that letter is not a vowel, keep prompting until a vowel is entered.

My desired outcome is:

Enter a vowel:z

That is not a vowel. Try again:h

That is not a vowel. Try again:w

That is not a vowel. Try again:t

That is not a vowel. Try again:o

Congrats. You picked a vowel.

So far my code is:

do 
{
printf("Enter a vowel: ");
scanf(" %c", &letter);
}

while (letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o' && letter != 'u' && letter != 'A' && letter != 'E' && letter != 'I' && letter != 'O' && letter != 'U');
{
printf("That is not a vowel. Try again: ");
scanf(" %c", &letter);    
}

printf("Congrats. You picked a vowel!!!");

CodePudding user response:

Do and While should go together. So you are looking for a code that will be something like below. Your objective should be to ensure the user is in loop till a vowel is received. Also you can further improve the code by using functions for the vowel check. Find a sample below.

    do 
    {
        printf("Enter a vowel: ");
        scanf(" %c", &letter);
        if (letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o' && letter != 'u' && letter != 'A' && letter != 'E' && letter != 'I' && letter != 'O' && letter != 'U')
            printf("Sorry, Try again \n");
    } while (letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o' && letter != 'u' && letter != 'A' && letter != 'E' && letter != 'I' && letter != 'O' && letter != 'U');
    
    printf("Congrats. You picked a vowel!!!");
    }

The applicable loops in C are do-while, for and while. There is no do.

CodePudding user response:

Your braces, indentation and spacing are deceptive. It looks like the do has no while and that the while controls the block below it...

Try this:

/*...*/
    while( 1 ) {
        printf( "Enter a vowel: " );

        char letter;
        if( scanf( " %c", &letter ) != 1 )
            exit( -1 );

        if( strchr( "aeiouAEIOU", letter ) != NULL ) // found!
            break;

        printf( "That is not a vowel. Try again:\n\n" );
    }
    printf( "Congrats. You picked a vowel!!!\n" );
    // NB: 'letter' has "gone out of scope".
/*...*/

strchr() searches the string supplied for a matching character, returning NULL if it is NOT found (ie: not a vowel, in this case.)

The "infinite loop" can only "break" when the user supplies a vowel.

Add a few LFs (\n) to the print statements.

CodePudding user response:

Hopefully OP will see the problem once OP's code is properly formatted.

  do {
    printf("Enter a vowel: ");
    scanf(" %c", &letter);
  } while (letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o'
      && letter != 'u' && letter != 'A' && letter != 'E' && letter != 'I'
      && letter != 'O' && letter != 'U');

  {
    printf("That is not a vowel. Try again: ");
    scanf(" %c", &letter);
  }

There is no error message in the loop.

CodePudding user response:

A more concise way to achieve that would be using strchr(), as suggested by @tadman in the comments.

#include <stdio.h>
#include <string.h>

int main () {
    char * vowels = "aeiouAEIOU";
    char letter;
    do {
        printf("Enter a vowel: ");
        scanf(" %c", &letter);
    } while (strchr(vowels, letter) == NULL);

    printf("Congrats. You picked a vowel!!!");
}
  • Related