小编sid*_*d-m的帖子

Scanf不会第二次执行

我正在尝试: - 如果用户输入无效值,则重新读取该值.但问题只scanf()执行一次,不会执行任何其他时间,程序会陷入无限循环.

#include<stdio.h>
#include<math.h>
main()
{
    unsigned int a;
    unsigned int b = pow(2,M-1);
    unsigned int c;
    int x;

    printf("b = %i",b);

    input:
    fflush(stdin);
    fflush(stdout);
    printf("\nEnter any integer: ");
    x = scanf("%u",&a);

    printf("%u",a);
    if(x==0) 
        goto input;

    printf("\na = %i",a);

    c = a & b;

    printf("\nc = %i",c);

    if(c)
        printf("\nthe bit %i is set",M);
    else
        printf("\nthe bit %i is not set",M);
}
Run Code Online (Sandbox Code Playgroud)

我之前尝试使用添加空间,但%u也试过fflush(stdin)但没有任何效果.

编辑:我知道不建议使用goto,但我必须这样做.(使用循环不是一个选项).M是我在编译时使用gcc命令行定义的宏.

c goto scanf

5
推荐指数
1
解决办法
1714
查看次数

标签 统计

c ×1

goto ×1

scanf ×1