C++使用 dowhile 进行验证
C++ validation using dowhile
这是我
的代码,但如果用户输入要检查的值之一,它会拾取错误,但在无限循环中显示错误消息
std::cin >> withdrawAmount;
do
{
if (withdrawAmount < balance && withdrawAmount > 0)
{
break;
}
std::cout << "Error: You have insufficient balance to withdraw " << withdrawAmount << " pounds. Your balance is only " << balance << " pounds." << std::endl;
std::cout << "Or you have entered a non positive integer" << std::endl;
}
while (true);
// some more code
而这段代码可以正常工作,对我来说看起来是一样的
std::cout << "Please enter todays date: (as an integer) ";
do
{
std::cin >> date;
if (date > 1 && date < 31)
{
break;
}
std::cout << "Error: Please enter todays date: (as an integer) ";
}
while (true);
// some more code
此外,如果有人可以向我展示如何添加验证以检查他们输入的内容实际上是一个整数(例如不是"两个",那就太棒了)
对于您的主要问题,cin 在循环之外,因此它只出现一次。
对于您的第二个问题,请在提取后调用 std::cin 的 fail() 成员函数。
相关文章:
- 正在尝试了解输入验证循环
- 如何在C++中检查2D数组中负值的输入验证
- LibGit2 SSH身份验证失败
- Vulkan验证层不断在VkQueuePresentKHR()上抛出图像布局错误
- constexpr上下文中std::initializer_list的验证
- 正在验证c++中用户的整数输入
- 加密++验证大文件签名
- C++卡验证问题
- 验证指针链
- 此代码验证公式是什么意思?
- cin 的十进制输入验证?
- 简单的 HTML 验证器
- 密码长度验证 (c++)
- 输入验证以筛选出字符、字符串和一系列整数
- 如何使用 CredUIPromptForWindowsCredentials验证凭据
- 指针验证的最佳实践?
- RSA_PKCS1签名验证
- 验证openssl c++中的签名,这是由JAVA DSA签名的?
- 使用 C++ 验证 NMEA 句子
- C++使用 dowhile 进行验证