开发者

Equation not working correctly in C++ [closed]

开发者 https://www.devze.com 2023-04-04 18:44 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago. 开发者_开发问答
//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.


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.

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: 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;

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

0

精彩评论

暂无评论...
验证码 换一张
取 消