关于循环的问题 [英] question about for loop

查看:103
本文介绍了关于循环的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们希望用户不能输入负值时可以使用for循环


我们想限制用户输入负值

Can we use for loop when we want user can not enter negative value


we want to restrict user to enter negative values

推荐答案

是的,您当然可以,但这并不是很有意义.您可以使用for循环进行循环,直到输入了非负数,然后再进入break循环.唯一的问题是 为什么 为什么 为什么 ?

问候,

Manfred
Yes of course you can, not that it would make much sense though. You could use a for loop to loop until a non-negative number was input and then break out of the loop. Only the question would be why, why, why?

Regards,

Manfred


Manfred的解决方案:):
The Manfred''s solution :) :
void WaitForPositiveInput(int& iValue)
{
  enum { eInitValue = -1, eMinValue = 0 };
  iValue = eInitValue;

  for (cin >> iValue; iValue < eMinValue; cin >> iValue);
}


这篇关于关于循环的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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