2011-08-25 65 views
0

有人能幫我解決這個問題嗎? 我已經把我的頭腦花了一個多小時,我無法讓它工作。 這是C++,我一直在學習一點點,但我還是新...我無法編寫控制檯應用程序來創建一個簡單的程序來解決養老金的數學公式

int main() 
{ 
double rate, amount,time, S; 

    cout << "Insert the time of the super: "; 
    cin >> time; 

    cout << "Insert the rate (as a decimal, eg 1% AKA 101% = 1.01): "; 
    cin >> rate; 

    cout << "Insert the amount $: "; 
    cin >> amount; 

    S =("amount * (rate^time - 1)", pow(rate,time)); 
    cin >> S; 

    cout << "The total amount is: " << "S /(rate - 1)" << endl; 

    system("PAUSE"); 
    return 0; 
} 

我沒有得到一個編譯錯誤,但我永遠不能從它

回答

4

得到答案你「從來沒有得到一個結果」,因爲你與逗號操作古怪設定S1到pow結果然後用線

cin >> S; 

正等待您輸入另一個號碼再次分配給它。

你有兩個主要問題。以下是更新後的代碼與改變的部分評論:

int main() 
{ 
    double rate, amount,time, S; 

    cout << "Insert the time of the super: "; 
    cin >> time; 

    cout << "Insert the rate (as a decimal, eg 1% AKA 101% = 1.01): "; 
    cin >> rate; 

    cout << "Insert the amount $: "; 
    cin >> amount; 

    S = amount * pow(rate, time - 1); // take away the quotes and don't make pow seperate 

    cout << "The total amount is: " << (S /(rate - 1)) << endl; // do the calculation and output it 

    system("PAUSE"); 
    return 0; 
} 

記住,引號裏的"like this"東西都是字符串常量,所以"4 * 4"是一個字符串,但4 * 4(見沒有引號),並乘其產生數量16

+0

我認爲-1應該被分組爲pow(rate,time-1)而不是pow(rate,time)-1。我可能會錯誤的這個,但是。 – templatetypedef

+0

@模板是啊我認爲你是對的,它看起來就是這樣,mea culpa。 –

0

我不認爲你應該給你的方式賦值。 S被聲明爲double,並且您最初正在爲其分配一個字符串。當你輸出結果時,你也將計算用引號括起來。您應該簡單地輸入:< < S /(rate-1); //沒有引號或者cout就會輸出字符串

+0

他沒有給它分配一個字符串,他分配了一個double,因爲'pow'返回一個double,而逗號操作符「丟棄」第一個表達式。 –

+0

@seth卡內基感謝您的更正! – Icarus

相關問題