`A ^ B`

performs an XOR of `A`

and `B`

which is not the same as `A`

^{B}

For "to the power of" you would need

C = (int) pow((double) A,B);Your error may disappear if you use it.

See more:

/* This program is used to calculate the Compound Interest, given the necessary parameters*/ #include <iostream> using namespace std; #include <iomanip> using std::setw; int main () { int principal, rate, time, n, a; //the Rate should be declared as a float or Double to that it can accommodate decimal values because rate should be in decimal cout << "Enter the Principal \n"; cin >> principal; cout << "Enter the Rate \n"; cin >> rate; cout << "Enter the Time \n"; cin >> time; cout << "Enter the Number of Years \n"; cin >> n; a = principal*(1+rate/n)^n*time; cout << "The Compound Interest for " << n << " months is " << a << "\n"; return 0; }

I get error message "Error: expression must have integral or enum type" from the code above when I try to run it.

I'm trying to write a program that calculate the compound Interest

NB: The problem is in the rate. The data type for the rate should be float or double so that it can accommodate decimal. The code works if I declare all the variables as integers

I have tried to declare all the variables as float or double but it doesn't work.

Um...you do realise that the ^ operator is not "to the power of", don't you?

^{B}

For "to the power of" you would need

`A ^ B`

performs an XOR of `A`

and `B`

which is not the same as `A`

For "to the power of" you would need

C = (int) pow((double) A,B);Your error may disappear if you use it.

Permalink

Share this answer

v2

C++

int principal, rate, time, n, a; //the Rate should be declared as a float or Double to that it can accommodate decimal values because rate should be in decimal

Why don't you do what is in comment ?

C++

a = principal*(1+rate/n)^n*time;

`^`

is not related to power or exponent, this is the bitwise xor operator.Where is the error?Quote:I get error message "Error: expression must have integral or enum type" from the code above when I try to run it.

Permalink

Share this answer

The problem is your use of a logical operator in your expression:

The

C++

a = principal*(1+rate/n)^n*time;

The

`^`

operator in C/C++ is bitwise XOR, not power. You need to use one of the pow, powf, powl[^] functions.
Permalink

Share this answer

You should use the datatype float or double for such operation to get a mathematical precision.

Rethink the strategy of calculating the amount. I know it as empowering the added rate for years. If the rate is in percent you must divide by 100. (As I coded).

Your time input is used. That you must solve for yourself.

Maybe you add a hint that the input can have some fracton seperated with a "."

Rethink the strategy of calculating the amount. I know it as empowering the added rate for years. If the rate is in percent you must divide by 100. (As I coded).

Your time input is used. That you must solve for yourself.

C++

float principal, rate, time, n, a;//or double a = principal*pow(1+rate/100,n);

Maybe you add a hint that the input can have some fracton seperated with a "."

Permalink

Share this answer

v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900