2011-09-15 5 views
-3

可能性の重複:このコードでは
Equation not working correctly in C++
Help with POW function in C++Cは、varが丸めフロート++

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

#include <iostream>  //Input/output 
#include <cmath>  //Math Functions 

using namespace std; 

int main() 
{ 
    float P, R, T, S, I;                  //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; 
    S=pow(1+R/T,T)*P; //Equation to find Savings 
    I=S-P;    //Equation to find interest in $ 
    cout<<"Interest Rate: " << R*100 <<"%" ; 
    cout<<endl; 
    cout<<endl; 
    cout<<"Times Compounded: " << T; 
    cout<<endl; 
    cout<<endl; 
    cout<<"Principal: $" << P; 
    cout<<endl; 
    cout<<endl; 
    cout<<"Interest: $" << I; 
    cout<<endl; 
    cout<<endl; 
    cout<<"Ammount in Savings: $" << S; 
    cout<<endl; 
    cout<<endl; 
    return 0; 
} 

最終的な出力数ラウンドを作るための方法がありますたとえ0であっても小数点以下2桁まで?

+2

[何を試しましたか?](http://mattgemmell.com/2008/12/08/what-have-you-tried) –

+0

[setprecision](http://www.cplusplus。 –

+0

私は知りませんでした、私は始めることができました、先生は先に働いています。 –

答えて

5

私はcoutのことについて何も知らないとしましょう。私がcoutについて何も知らないので、私は "C++ cout"の検索エンジンクエリーを行います。最初のページにあるsuch as this MSDN doucmentationのリンクの1つをクリックします。そのページによると、coutostreamで、標準出力に出力されています。だからostreamso conveniently provided in the MSDN documentationを見てみましょう。 ostreamページには、a link called "iostream Programming"があります。それは有望に見えるので、それをクリックしてみましょう。

"iostreamプログラミング"ページにa link called "Output Streams"があります。再び、それは有望に見えます。結局のところ、私たちは画面に何かを出力しているので、見てみましょう。そのページには、a link called "Using Insertion Operators and Controlling Format"があります。私たちが近づいているように聞こえる。

私は「フォーマットを制御する方法」を示すページを見つけました。このページには、機能「setprecision() and setiosflags() complete with code examples」を記述する「Precision」というセクションがあります。ドキュメンテーションによると、恐らくあなたの問題を解決するかもしれません。

私が取った上記のプロセスは、インターネットのメンバーによってよく口語的に呼び出されるものです。"RTFM"これはあなた自身の情報を得るための非常に便利なテクニックであり、同僚より先に進むためにあなたの利点に慣れることができます。

+0

Silicoで何を言っているのか(ラウンドアバウトでユーモラスな方法で)setprecisionとsetwidthとiomanipヘッダーファイルを検索することです。そのような場合にはあまりにも鈍いです:-) – paxdiablo

+0

ユーザーがいつも同じ場所値で数字を入力することが分かっていれば、それは完璧です。プリンシパルは、1,000ドルまたは1,000,000ドルを入力することができます。私は彼らが1,000を入力するための精度を設定する場合、彼らは1,000,000を入力した場合、それをカットオフ –

+0

私はそれを持っていると思う、私は小数点以下の後に数をカウントするsetprecision –