方程在C ++中无法正常工作 [英] Equation not working correctly in C++

查看:104
本文介绍了方程在C ++中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//Samuel LaManna
//Program 1 (intrest rate)
/*Variables:
Principal=P
Interest Rate=R
Times Compounded=T
Answer=A */

#include <iostream>                                                                        //Input/output

using namespace std;

int main ()
{
  int P, R, T, A;                                                                          //Declaring Variables
  cout<<endl;
  cout<<"Interest Earned Calculator";                                                      //Prints program title
  cout<<endl;
  cout<<endl;
  cout<<"Please enter the Principal Value: ";
  cin >> P;
  cout<<endl;
  cout<<endl;
  cout<<"Please enter the Interest Rate (in decimal form): ";
  cin >> R;
  cout<<endl;
  cout<<endl;
  cout<<"Please enter the Number of times the interest is compounded in a year: ";
  cin >> T;
  cout<<endl;
  cout<<endl;
  A=P*((1+R)/T)^T;
  cout<<"Interest Rate", cout<<R;
  return 0;
}



当你得到它的方程式并开始输出它得到所有混乱并扔在一起。它是一个简单的利息计算器应用程序。

When it gets to were it does the equation and starts outputting it gets all messed and thrown together. Its a simple interest calculator app.

推荐答案

我会保证,如果你做利率计算需要浮点精度),您应该使用 int 数据类型。

I'll guarantee that, if you're doing interest rate calculations (or anything else requiring floating point accuracy), you should not be using an int data type.

另外 ^ 不是幂运算符,它是异或运算符。您需要查看 pow 标准库函数。

In addition ^ is not the power operator, it's the exclusive-or operator. You need to look into the pow standard library function.

这应该是足够让你找出为什么没有我为你做所有的工作。

That should hopefully be enough for you to figure out why your homework is misbehaving, without me doing all the work for you.


Aside:结构


cout<<Interest Rate,cout<<< R< / code>


是非常罕见的一种。它可以工作(我不知道从头顶是否逗号运算符是一个序列点,我太懒了,现在看看它),但你应该可能喜欢更常用的方式:


cout<< 利率:< R < endl;

Aside: The construct

cout<<"Interest Rate", cout<<R;

is a pretty rare one. It may work (I don't know off the top of my head whether the comma operator is a sequence point and I'm too lazy to go look it up at the moment), but you should probably prefer something like the more usual:

cout << "Interest Rate: " << R << endl;

您可能想要输出 A (答案): - )

And you probably want to output A (the answer) at some point :-)

这篇关于方程在C ++中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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