Home > Software engineering >  Why this do while loop is infinite after it take false?
Why this do while loop is infinite after it take false?

Time:05-23


After entering a value except 1,2,3,4 It will perform an infinite loop. Why is that guys?


#include<stdio.h>

int main(void)
{
    int person, vote1=0, vote2=0,vote3=0,vote4=0;

    do{

        printf("Select person to be voted!\n");
        printf("Chanaka     - Press 1\n");
        printf("Prasanna    - Press 2\n");
        printf("Tharindu    - Press 3\n");
        printf("Sandaruwan  - Press 4\n");
        printf("\n");
        printf("Quite       - Press Q\n");
        printf("\n");
        printf("\n");

        printf("Enter Number:");
        scanf("%d",&person);

        switch(person)
        {
            case 1: vote1  ; break;
            case 2: vote2  ; break;
            case 3: vote3  ; break;
            case 4: vote4  ; break;
        }
    }while(person == 1 || person== 2 || person ==3 ||person==4 );


    printf("\n");
    printf("\n");

    printf("Chanaka     - %d Votes\n", vote1);
    printf("prasanna    - %d Votes\n", vote2);
    printf("Tharindu    - %d Votes\n", vote3);
    printf("Sandaruwan  - %d Votes\n", vote4);

}

CodePudding user response:

With the information available it seems like your code works as intended. If you put a 5 the loop ends. If you put a Q the loop ends too. Typing 1,2,3 or 4 just adds a vote and continues with the loop which I understand is what you want the code to do.

Is there something missing in your question? If so please add it to your post.

  • Related