I need help writing a Loop that finds vowels in C - c

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

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.

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.

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.

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

how can I make this run multiple times using do-while loop?

I'm trying to make a program that will scan the character I entered and will end only if the character inputted is a vowel or if the number of inputted characters has already reached 5
when I executed the code it only run once, and even if I inputted a vowel it does not terminate/end.
I tried adding c++ but it did not change anything
I feel like something is missing but I can't figure it out.
I've also tried watching tutorials about do-while loop.
here it is:
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c",&c);
do {
printf("%c",c);
} while (c == 'a' && c == 'e' && c == 'i' && c == 'o' && 'u' && c == 'A' && c == 'E' && c == 'I' && c == 'O' && 'U' && c == 5);
return 0;
}
Maintain a count and check if count < 5 and it is not any vowel
string ch ;
cin >> ch ;
int count = 1 ;
while(count < 5 && ch != "A" && ch!= "E" && ch != "I" && ch!= "O" && ch != "U" && ch != "a" && ch!= "e" && ch != "i" && ch!= "o" && ch != "u" ){
cout << ch << '\n';
cin>>ch;
count ++ ;
}

While loop in C prints the same line more than once

char ch;
int nr=0;
printf("\n: ");
ch = getchar();
while(ch != 'q' && ch != 'Q'){
ch = tolower(ch);
if(ch == 'a' || ch == 'e' || ch == 'o' || ch == 'i' || ch == 'u')
nr++;
printf("something");
ch = getchar();
}
printf("vocale: %d", nr);
its supposed to count the number of vowels until the user presses q or Q. it's such a silly program and yet i cant get past it.
Instead of using getchar
ch = getchar();
that also reads white space characters use scanf like
scanf( " %c", &ch );
Pay attention to the leading space in the format string. It allows to skip white space characters.
For example
while ( scanf( " %c", &ch ) == 1 && ch != 'q' && ch != 'Q'){
Also it will be more safer to write
ch = tolower( ( unsigned char )ch );
The problem is, that the input only gets flushed to your program whenever the user presses enter. Another reason why it seems not to work is, because you don't have a newline at the end of you output (printf("vocale: %d", nr); ), which causes the output not to be flushed to the terminal when the program ends. Fix this and your program works, but maybe not as you expect it to, because you still have to press enter. It will still only count to the first 'q' found.
int main() {
char ch;
int nr = 0;
printf(": ");
while(tolower(ch = getchar()) != 'q'){
ch = tolower(ch);
if(ch == 'a' || ch == 'e' || ch == 'o' || ch == 'i' || ch == 'u')
nr++;
}
printf("vocale: %d\n", nr);
}
The program:
: aeu q oi (here I pressed enter)
vocale: 3

how to put if..else statement in do..while loop c program

so I have been working on my assignment and I can't figure out how to put if...else statement in my do..while loop because want I run the program it doesn't loop. the output is like this.
[this the output that I got][1]
#include <stdio.h>
#include <ctype.h>
int main(void){
char alphabet;
char confirm;
int lowercase_vowel, uppercase_vowel;
int a =1;
do{
printf("Enter an alphabet: ");
scanf("%c", &alphabet);
// evaluates to 1 if variable c is a lowercase vowel
lowercase_vowel = (alphabet == 'a' || alphabet == 'e' || alphabet == 'i' || alphabet == 'o' || alphabet == 'u');
// evaluates to 1 if variable c is a uppercase vowel
uppercase_vowel = (alphabet == 'A' || alphabet == 'E' || alphabet == 'I' || alphabet == 'O' || alphabet == 'U');
if(!isalpha(alphabet))
{
printf("Error! Non-alphabetic character.\n");
}
else if(lowercase_vowel || uppercase_vowel)
{
printf("%c is a vowel.\n", alphabet);
}
else
{
printf("%c is a consonant.\n", alphabet);
}
printf("\nif u want to proceed enter 1, if not enter 0\n");
scanf("%d", &a);
}while( a == 1);
}
``
[1]: https://i.stack.imgur.com/oRK1G.png
do
{
// code
}while (a = 1);
This will create an infinite loop, because it will assign 1 to a, and because a is now a nonzero value, the condition is true, and it will loop again:
Maybe what you want is:
do
{
// code
}while (a == 1); // comparison, not assignment
Also:
scanf("%d", a);
should be:
scanf("%d", &a);

How can we ignore the enter button press as a character in c programming at the time of taking input from user?

Look at the example:
#include<stdio.h>
int main()
{
char ch;
while(scanf("%c", &ch))
{
if(ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u' || ch == 'A' ||
ch == 'E' || ch == 'I' || ch == 'O' ||
ch == 'U')
{
printf("It's Vowel\n");
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("It's Consonant\n");
}
else
{
printf("Wrong Input/ It's not Alphabet\n");
}
}
return 0;
}
After compiling this example code, when I enter 'a', The output is "It's Vowel" and "Wrong Input/ It's not Alphabet". I think the cause for this output is, the compiler takes the character also takes the enter press as a character.
Is there any way to solve this problem?
I think the cause for this output is, the compiler takes the character also takes the enter press as a character.
It's not the compiler who takes characters. Getting input is a run-time operation. When the program is already running, the work of the compiler is far done, but beside that your guess is correct. It is because scanf() doesn't consume the newline character with made by the press to Enter at the first step.
This newline character then get read at the next iteration by scanf("%c", &ch)) and as the newline character is a legit character it is stored inside of ch.
Is there any way to solve this problem?
Use
while(scanf(" %c", &ch))
instead of
while(scanf("%c", &ch))
Notice the white space character (' ') before %c. This will fetch the abandoned newline character left in stdin from the last iteration.

Programming in C how to output inputted string onto the screen?

I am trying to to have my program output the words inputted into my program onto the screen. So far, my program outputs random characters depending on what I type. For example, if I input the word hey, it outputs on the screen %. How do I go about fixing this to output the word hey on the screen? My code is down below.
#include <stdio.h>
#include<conio.h>
int main(){
int word;
char cont;
for (;;){
int countword = 0;
int countpunct = 0;
printf("\nEnter the String: ");
while ((word = getchar()) != EOF && word != '\n'){
if (word == ' ' || word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&'){
countword++;
}
if (word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&'){
countpunct++;
}
}
printf("%c", word);
printf("\nThe number of words is %d.", countword);
printf("\nThe number of punctuation marks is %d.", countpunct);
printf("\nContinue? Y/N?");
scanf("%c", &cont);
if (cont != 'y' && cont != 'Y'){
return 0;
}
}
}
You are using & with printf(). It will print the address of the variable (and not its value)!
Do this instead:
printf("%c", word); // notice there is no &
Apart from that, I noticed a few things in your code worth mentioning:
word is declared as an int, but is read and printed as a char. Why?
Your while loop makes it sure that word holds either EOF or \n while it exits the while loop.
while ((word = getchar()) != EOF && word != '\n')

Resources