错误:二元运算符的类型为'double'和'double'的操作数无效 [英] error: invalid operands of type 'double' and 'double' to binary operator
问题描述
我试图做以下问题,但我不断得到double和double类型的无效操作数的错误到二元运算符。我已经用我超级有限的能力切换了我能想到的一切,但仍然无法修复错误。谁能帮我? (我在底部发布了我正在编写代码的问题)。非常感谢任何可以提供帮助的人
*编辑*我注意到当我用int切换双重代码时,我需要能够使用小数点当我使用浮动时,同样的错误发生在我身上
I was trying to do the following problem but I keep getting the error of invalid operands of type double and double to binary operator. I've switched everything I can think of with my super-limited abilities and still can't fix the error. Can anyone help me? (I posted the question at the very bottom that I am doing the code for). Thank you very much to anyone who can help
*edit* I noticed when i switch the double with int the code works but i need to be able to use decimal points.the same error happens to me when i use float
#include <cstdlib>
#include <iostream>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
double principal1;
double rate;
double T;
double amount;
cout << "Enter principal1: ";
cin >> principal1;
cout << "Enter rate: ";
cin >> rate;
cout << "Enter times compounded annually: ";
cin >> T;
amount=(principal1)*((1+(rate/T))^T);
cout << "The total amount is : " << amount << endl;
return 0;
}
假设除原始投资外没有其他存款,余额为一年后的储蓄账户可以计算为
金额=委托人*(1 +利率/ T)^ T
本金是储蓄账户中的余额,比率是利率,T是利息在一年内复利的次数(如果利息按季度复利,则T为4)。
写一个要求本金,利率的程序,以及利息复合的次数。它应该显示类似的报告
编程挑战145
$ b $b兴趣费率:4.25
时间复合:4
本金:1000
利息:43.34
储蓄金额:1043.34
Assuming there are no deposits other than the original investment, the balance in a savings account after one year may be calculated as
Amount = Principal*(1 + Rate/T)^ T
Principal is the balance in the savings account, Rate is the interest rate, and T is the number of times the interest is compounded during a year (T is 4 if the interest is compounded quarterly).
Write a program that asks for the principal, the interest rate, and the number of times the interest is compounded. It should display a report similar to
Programming Challenges 145
Interest Rate:4.25
Times Compounded: 4
Principal:1000
Interest:43.34
Amount in Savings:1043.34
推荐答案
你没有显示编译错误所在的行。行amount =(principal1)*((1+(rate / T))^ T);
这里有一个错误你不能在这里使用XOR ^运算符。如果您打算使用电源功能,请使用: http://www.cplusplus.com/reference/cmath/pow [ ^ ]。
另请参阅:
https:// msdn.microsoft.com/en-us/library/3akey979.aspx [ ^ ]。
编写代码而不阅读所用语言的基础知识会产生相反的效果。是不是很容易查阅一些参考网页(在这种情况下是MSDN)或书?我的建议是认真对待编程。
-SA
You did not show the line where the compilation error is. There is an error in the lineamount=(principal1)*((1+(rate/T))^T);
you cannot use XOR ^ operator here. If you meant to use power function, please use: http://www.cplusplus.com/reference/cmath/pow[^].
See also:
https://msdn.microsoft.com/en-us/library/3akey979.aspx[^].
It's very counter-productive to write code without reading on the basics of the language you use. Isn't it so easy to consult some reference Web page (MSDN, in this case) or a book? My advice is to get to programming seriously.
—SA
谢谢,我切换了我的代码如下,并使其工作。我不熟悉指数,现在我对他们很好
#include< cstdlib>
#include< iostream>
#include< cmath>
使用命名空间std;
/ *
*
* /
int main(){
float principal1; //首席执行官
浮动利率; //利率
浮动T; //每年复合次数
浮动金额; //账户最终金额
浮动助手;
cout<< 输入principal1:;
cin>> principal1;
cout<< 输入率:;
cin>>率;
cout<< 输入时间每年复合:;
cin>> T;
helper =(1+(rate / T)); / *我不知道如何正确使用指数,所以我做了一个辅助变量的东西* /
amount =(principal1)* pow(helper,T);
cout<< 一年后的平衡是:<<数量<<结束;
返回0;
}
Thank you , i switched my code to the following and made it work. I was unfamiliar with exponents and now I'm good with them
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
/*
*
*/
int main() {
float principal1; //starting principal
float rate; //interest rate
float T; //times compounded annually
float amount; //final amount in account
float helper;
cout << "Enter principal1: ";
cin >> principal1;
cout << "Enter rate: ";
cin >> rate;
cout << "Enter times compounded annually: ";
cin >> T;
helper=(1+(rate/T)); /*I didn't know how to properly use exponents with this so i made a helper variable thing */
amount=(principal1)* pow(helper,T);
cout << "The balance after a year is : " << amount << endl;
return 0;
}
这篇关于错误:二元运算符的类型为'double'和'double'的操作数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!