Question

[Solved] Error message displaying as “expression must have integral or enum type” in c++

I have the following code and I am getting the error at this equation:

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

Please help me to find the reason for this error.

# 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(); 
}

Solution #1:

C++11 5.12 – Bitwise exclusive OR operator

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.


If you want to compute v=(p)*(1+r)n, you need to change

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

to

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

In C++, ^ is XOR (exclusive or) operator, e.g. a = 2 ^ 3; // a will be 1.

Check out here for more info.

Respondent: herohuyongtao

Solution #2:

The problem is that ^ is not an exponential math operator in C++, instead it is a bitwise xor operation. Bitwise operations can only be done on integral / enum values.

If you want to raise a floating point to a specific power use the powf function

powf(p * (1 + r), n)

// Or possibly the following depending on how you want the
// precedence to shake out
p * powf(1 + r, n)
Respondent: JaredPar

Solution #3:

The bitwise exclusive OR operator ^

applies only to integral or unscoped enumeration operands

(The C++ Standard)

Respondent: Vlad from Moscow

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy