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

Time:11-16

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

CodePudding user response:

It looks like your while condition is assigning the value 1 to a.

Try:

while( a == 1);

CodePudding user response:

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);
  • Related