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

2021/8/26 6:06:28

本文主要是介绍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;
}

 



这篇关于c语言 输入验证(限制输入正数)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程