计算复利 [英] To calculate compound interest

查看:94
本文介绍了计算复利的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/* 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;
}



当我尝试运行它时,我从上面的代码中收到错误消息错误:表达式必须具有整数或枚举类型。

我正在尝试编写一个计算复合利息的程序

注意:问题在于速率。速率的数据类型应为float或double,以便它可以容纳小数。如果我将所有变量声明为整数,则代码有效



我尝试过的方法:



我试图将所有变量声明为float或double但它不起作用。


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.

推荐答案

嗯......你确实意识到^运算符不是权力,不是吗?

A ^ B 执行的XOR> A B A <$ c $不同c> B

对于能力你需要

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.


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 ?

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



^ 与幂或指数无关,这是按位xor运算符。


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

错误在哪里?


问题是你在表达式中使用逻辑运算符:

The problem is your use of a logical operator in your expression:
a = principal*(1+rate/n)^n*time;



^ C / C ++中的运算符是按位异或,而不是幂。您需要使用 pow,powf,powl 之一[ ^ ]函数。


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


这篇关于计算复利的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆