如何强制用户输入正整数? [英] How do I force user to input a positive integer?

查看:253
本文介绍了如何强制用户输入正整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

强制用户输入正整数,并将用户置于循环中,直到它们成功。

Force user to input a positive integer and put user in a loop till they do.

因此,我希望一切都包括不允许超过> 0

So I want everything including characters not allowed just over > 0

我尝试了以下操作:

while (i < 0) do { 
    printf("please input a number that's positive"); 
    scanf("%d", i); 
}


推荐答案

int i;
do 
{ 
  printf("please input a number that's positive"); 
  scanf("%d", &i); 
}while (i < 0); 

c语言不提供用户输入的错误检查。用户需要输入正确的数据类型。例如,如果用户在期望整数值时输入字符,则程序可能进入无限循环或异常中止。

The c language provides no error checking for user input. The user is expected to enter the correct data type. For instance, if a user entered a character when an integer value was expected, the program may enter an infinite loop or abort abnormally.

这篇关于如何强制用户输入正整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆