#incluof <stdio.h>
int main ()
{
int a, e, i, o, u, x;
char letter;
a = e = i = o = u = 0;
for (x = 0; x <= 20; x++)
{
printf("Enter character: ", x);
scanf("%c", & letter);
if (letter == 'a')
{a = a + 1;}
elseif (letter == 'e')
{e = e + 1;}
elseif (letter == 'i')
{i = i + 1;}
elseif (letter == 'o')
{o = o + 1;}
elseif (letter == 'u')
{u = u + 1;}
else
{}
}
printf("Number of 'a': %d\n", a);
printf("Number of 'e': %d\n", e);
printf("Number of 'i': %d\n", i);
printf("Number of 'o': %d\n", o);
printf("Number of 'u': %d\n", u);
fflush (stdin);
getchar ();
return 0;
}
Everything seems fine, but, when it gets to the for, it only allows to introduce the characters when the variable in the for loop is an odd number.
I would like to know how I could fix this. Thanks, guys!
When you press enter, the '\n' in entered into stdin's buffer, so the next time it reads it reads that and runs the loop on it. You might want to simply check for a newline character and not increment the variable in the for loop if that's the case.