Home > database >  Why my while loop in not getting repeated in this switch case?
Why my while loop in not getting repeated in this switch case?

Time:03-30

#include <stdio.h>
int main()
{
    int a;
    scanf("%d", &a);
    int if_again = 1;
    int c = 1;
    switch (a)
    {
    case 1:
        while (if_again == 1)
        {
            scanf("%d", c);
            if (c == 1)
            {
                if_again == 1;
            }
            else
            {
                if_again == 0;
            }
        }
    }
    return 0;
}

It is getting terminated after giving 1 two times only, but it should keep on getting repeated until 0 is entered. What is wrong with my code?

CodePudding user response:

scanf needs the base address of a variable you give it. So scanf("%d, c); needs to be scanf(%d, &c);

The following code is also wrong:

if (c == 1)
{
   if_again == 1;
}

The compiler thinks that you are comparing "if_again" with 1. I think you are trying to set the variable to 1? In that case do this:

if (c == 1)
{
   if_again = 1;
}

The same is true for the else statement.

Also, why is there a switch statement? If you want to see it a is 1, then just to an if

  • Related