scanf ignoring, infinite loop

后端 未结 6 1029
遥遥无期
遥遥无期 2020-11-27 22:54
int flag = 0;
int price = 0;
while (flag==0)
{
    printf(\"\\nEnter Product price: \");
    scanf(\"%d\",&price);
    if (price==0) 
        printf(\"input not          


        
6条回答
  •  醉话见心
    2020-11-27 23:15

    It goes into an infinite loop because scanf() will not consumed the input token if match fails. scanf() will try to match the same input again and again. you need to flush the stdin.

    if (!scanf("%d", &sale.m_price)) fflush(stdin);

提交回复
热议问题