gpt4 book ai didi

c++ - 错误 : lvalue required as left operand of assignment when testing divisibilty

转载 作者:行者123 更新时间:2023-11-30 03:19:38 25 4
gpt4 key购买 nike

在c++上,我想说如果输入的量能被20整除,那么就输出解。但是,它说第二行的操作数必须在左边。提前致谢!

  cin >> amountEntered;
if (amountEntered / 20 = 0)
{
cout << amountEntered/20;
}

这是 c++ 给出错误的原因:

main.cpp:16:28: error: lvalue required as left operand of assignment
if (amountEntered / 20 = 0)
^

此外,如果我想说输入的金额是否不能被 20 整除,是不是“amountEntered/20 == 0”?

最佳答案

= 表示赋值,这会导致错误,因为没有任何东西可以赋值。相等性测试是 ==

另外,你在这里做整数运算。所以小于 20 的任何值都将导致 0。所以最好测试 modulo :

if (amountEntered % 20 == 0) 

关于c++ - 错误 : lvalue required as left operand of assignment when testing divisibilty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53575775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com