c语言 输入验证(限制输入正数)

 

#include <stdio.h>

int main(void)
{
    long num;
    
    printf("please input an positive num: ");
    
    while(1)
    {
        while(scanf("%ld", &num) != 1)
        {
            printf("please input  an num. try again.\n");
            while(getchar() != '\n')
                continue;    
        }
        if(num <= 0)
        {
            printf("please input an positive.\n");
            continue;
        }
        if(num > 0)
        {
            printf("num- num: %d.\n", num);
        }
        
        int trigger;
        printf("please choose to exit(100): ");
        scanf("%d", &trigger);
        printf("trigger: %d.\n", trigger);
        if(trigger == 100)
            break;
        while(getchar() != '\n')
            continue;
        
        printf("please input an positive num: ");
        
    }
        
    return 0;
}

 

posted @ 2021-08-26 01:06  小鲨鱼2018  阅读(350)  评论(0编辑  收藏  举报