-1

"Else condition is executing two times and printing two times in while loop,if-else is not working properly,"

"I also tried the goto statement, I get the same problem"

#include<stdio.h>
#include<conio.h>
int main ()
{
  int count=0;
  char select;
  while(count==0)
  {
    printf("please select options\n");
    scanf("%c", &select);
    if (select=='b' || select=='B')
    {
        printf("you have selected b");
        count=1;
    }
    else
    {
      printf("why this line printing two time\n");
    }
  }
 return 0;
}

result

please select options
g
why this line printing two time
please select options
why this line printing two time
please select options

"When I take input other than 'b'/'B', I expect out to print the else condition and again run while loop, but the actual output is printing else condition two times"a result of my code

iamaksingh11
  • 55
  • 10

1 Answers1

3

When you use scanf to get a character you should use

scanf(" %c, &c);

Instead of

scanf("%c, &c);

Leaving a blank space tells to scanf to skip optional white space.

LucasFab
  • 95
  • 10