15,992,250 members
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

What I have tried:

I have tried to declare all the variables as float or double but it doesn't work.
Posted
Updated 16-Jan-17 0:32am
v2

## Solution 1

Um...you do realise that the ^ operator is not "to the power of", don't you?
A ^ B performs an XOR of A and B which is not the same as AB
For "to the power of" you would need
C = (int) pow((double) A,B);
Your error may disappear if you use it.

v2

## Solution 2

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.
Quote:
I get error message "Error: expression must have integral or enum type" from the code above when I try to run it.
Where is the error?

## Solution 3

The problem is your use of a logical operator in your expression:
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.

## Solution 4

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.
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 "."

v2