Click here to Skip to main content
16,016,759 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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 "."
 
Share this answer
 
v2
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.
 
Share this answer
 
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?
 
Share this answer
 
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.
 
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