gpt4 book ai didi

c++ - 在 C++ 中显示为 "expression must have integral or enum type"的错误消息

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:00:37 26 4
gpt4 key购买 nike

我有以下代码,但在这个等式中出现错误:

v=p*(1+r)^n.

请帮我找出这个错误的原因。

# include <iostream>
# include <limits>

using namespace std;

int main()
{
float v,p,r;
int n;

cout<<"Enter value of p:";
cin>>p;
cout<<"Enter value of r:";
cin>>r;
cout<<"Enter value of n:";
cin>>n;

v=(p)*(1+r)^n; // here i am getting error message as "expression must have integral or enum type"

cout<<"V="<<v;

std::cin.ignore();
std::cin.get();
}

最佳答案

C++11 5.12 - 按位异或运算符

exclusive-or-expression: and-expression exclusive-or-expression ˆ and-expression 1 The usual arithmetic conversions are performed; the result is the bitwise exclusive OR function of the operands. The operator applies only to integral or unscoped enumeration operands.


如果你想计算v=(p)*(1+r)n,你需要改变

v=(p)*(1+r)^n;

v = p * powf(1+r, n); // powf: exponential math operator in C++

C++中,^XOR(异或)运算符,例如a = 2 ^ 3;//a 将为 1

查看 here了解更多信息。

关于c++ - 在 C++ 中显示为 "expression must have integral or enum type"的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20955318/

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