简化 if else 用户输入验证

发布于 2024-12-09 11:38:19 字数 784 浏览 0 评论 0原文

这可以完成我需要做的工作,但我想知道是否有一种更简单/更有效的方法来完成同样的事情。用户输入两个数字,它们需要在 0 到 50 之间,如果它不在所需的范围内,它就会结束

cout << "Enter the pixel coordinate (x, y): ";
cin >> usrInput1 >> userInput2;
if  (usrInput1 > 50)
{
    cout << "ERROR! 1" << endl;
    return 0;
}
else if (usrInput1 < 0)
{
    cout << "ERROR! 2" << endl;         
    return 0;
}
else if (usrInput2 > 50)
{
    cout << "ERROR! 3" << endl;
    return 0;
}
else if (usrInput2 < 0)
{
    cout << "ERROR! 4" << endl;
    return 0;
}
else
{
    cout << "Success" << endl;
    xvar = usrInput1 + usrInput2;
}

我试图做的事情

if(! 0 > userInput1 || userInput2 > 99)

,但显然没有成功。

感谢您的帮助

This gets the job done for what I need it to do, but I am wondering if there is an easier/more efficient way of accomplishing the same thing. The user inputs two numbers and they need to be between 0 and 50, if it doesnt fall within the required range it ends the prog

cout << "Enter the pixel coordinate (x, y): ";
cin >> usrInput1 >> userInput2;
if  (usrInput1 > 50)
{
    cout << "ERROR! 1" << endl;
    return 0;
}
else if (usrInput1 < 0)
{
    cout << "ERROR! 2" << endl;         
    return 0;
}
else if (usrInput2 > 50)
{
    cout << "ERROR! 3" << endl;
    return 0;
}
else if (usrInput2 < 0)
{
    cout << "ERROR! 4" << endl;
    return 0;
}
else
{
    cout << "Success" << endl;
    xvar = usrInput1 + usrInput2;
}

I was trying to do something like

if(! 0 > userInput1 || userInput2 > 99)

but obviously that didn't work out..

Thanks for any assistance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那支青花 2024-12-16 11:38:19
cout << "Enter the pixel coordinate (x, y): ";
cin >> usrInput1 >> userInput2;
if  ( (usrInput1 > 50) || (usrInput1 < 0)  ||
      (usrInput2 > 50) || (usrInput2 < 0) )
{
    cout << "ERROR!" << endl;
    return 0;
}
cout << "Success" << endl;
xvar = usrInput1 + usrInput2;

如果你真的想要的话,你实际上可以进一步组合它:

if  ((std::max(usrInput1,usrInput2) > 50) 
   || std::min(usrInput1,usrInput2) < 0))
{ 
     ...

在这种情况下,我宁愿有一个辅助函数

bool isValid(int i) { return (i>=0) && (i<=50); }

// ...
if (isValid(usrInput1) && isValid(usrInput2))
    ...

编辑考虑检查输入操作 - 这在OP中缺失:

<子>

if (!(cin >> usrInput1 >> userInput2))
{
     std::cerr << "input error" << std::endl;
}
if  ( (usrInput1 > 50) || (usrInput1 < 0)  ||
      (usrInput2 > 50) || (usrInput2 < 0) )
{
     std::cerr << "value out of range" << std::endl;
}

cout << "Enter the pixel coordinate (x, y): ";
cin >> usrInput1 >> userInput2;
if  ( (usrInput1 > 50) || (usrInput1 < 0)  ||
      (usrInput2 > 50) || (usrInput2 < 0) )
{
    cout << "ERROR!" << endl;
    return 0;
}
cout << "Success" << endl;
xvar = usrInput1 + usrInput2;

You could actually combine it further if you really wanted:

if  ((std::max(usrInput1,usrInput2) > 50) 
   || std::min(usrInput1,usrInput2) < 0))
{ 
     ...

in which case I would rather have a helper function

bool isValid(int i) { return (i>=0) && (i<=50); }

// ...
if (isValid(usrInput1) && isValid(usrInput2))
    ...

Edit Consider checking the input operations - this was missing in the OP:

if (!(cin >> usrInput1 >> userInput2))
{
     std::cerr << "input error" << std::endl;
}
if  ( (usrInput1 > 50) || (usrInput1 < 0)  ||
      (usrInput2 > 50) || (usrInput2 < 0) )
{
     std::cerr << "value out of range" << std::endl;
}

江湖正好 2024-12-16 11:38:19

我想我会把它的大部分移到一个函数中:

bool check_range(int input, 
                  int lower, int upper, 
                  std::string const &too_low, std::string const &too_high) 
{
    if (input < lower) {
        std::cerr << too_low << "\n";
        return false;
    }
    if (input > upper) {
        std::cerr << too_high << "\n";
        return false;
    }
    return true;
}

然后你会使用这样的东西:

if (check_range(usrInput1, 0, 50, "Error 1", "Error 2") &&
    check_range(usrInput2, 0, 50, "Error 3", "Error 4")
{
    // Both values are good
}

我应该补充一点,我对此并不完全满意。该函数确实体现了我宁愿分开的两个职责(检查范围并在超出范围时报告错误)。

I think I'd move most of it into a function:

bool check_range(int input, 
                  int lower, int upper, 
                  std::string const &too_low, std::string const &too_high) 
{
    if (input < lower) {
        std::cerr << too_low << "\n";
        return false;
    }
    if (input > upper) {
        std::cerr << too_high << "\n";
        return false;
    }
    return true;
}

Then you'd use this something like:

if (check_range(usrInput1, 0, 50, "Error 1", "Error 2") &&
    check_range(usrInput2, 0, 50, "Error 3", "Error 4")
{
    // Both values are good
}

I should add that I'm not entirely happy with this. The function does really embody two responsibilities that I'd rather were separate (checking the range and reporting the error if its out of range).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文