2014-09-23 94 views
-2

如果有人有一些建議 - 我可以利用幫助。該程序編譯,但沒有
正確計算。我曾經嘗試了很多次的嘗試,並且對此感到焦慮。有什麼建議麼?
我必須初始化所有這些變量?謝謝....C++計算器程序。問題

//This program mimics a calculator 
//***************************************** 

#include <iostream> 
#include <iomanip> 

using namespace std; 

int main() 
{ 
double firstint, secint, ansrlt, ansrlt2, ansrlt3, ansrlt4; 
char operat; 

cout << fixed << showpoint; 
cout << setprecision(4); 

cout << "This program mimics a standard calculator and outputs a result accurate to 4 decimal 
places." <<endl; 
cout << '\n'; 

cout << "Please enter 1st Integer to be calculated: "; 
cin >> firstint; 
cout << '\n'; 

cout << "Please enter operation to be performed in the form of *, /, +, - : "; 
cin >> operat; 
cout << '\n'; 

cout << "Please enter 2nd Integer to be calculated: "; 
cin >> secint; 
cout << '\n'; 

if (operat == '/' && secint == 0) 
    cout << "Division by 0 not allowed enter again." << endl; 

cin.get(); 

if (operat == '*') 
    ansrlt = firstint * secint; 
    cout << firstint << " " << operat << " " << secint << " = " << ansrlt << endl; 

cin.get(); 

if (operat == '/') 
    ansrlt2 = firstint/secint; 
    cout << firstint << " " << operat << " " << secint << " = " << ansrlt2 << endl; 

cin.get(); 

if (operat == '+') 
    ansrlt3 = firstint + secint; 
    cout << firstint << " " << operat << " " << secint << " = " << ansrlt3 << endl; 

cin.get(); 

    ansrlt4 = firstint - secint; 
    cout << firstint << " " << operat << " " << secint << " = " << ansrlt4 << endl; 

cin.get(); 

return 0; 
} 
+1

功課吧?殺手。它以什麼方式不能正確計算? – 2014-09-23 19:20:25

+0

Stackoverflow不是一個在線調試器... – Borgleader 2014-09-23 19:20:58

+0

我做了調試和編譯,沒有被拾取。答案只是每次輸出相同。所以例如2 + 2 = 4; 2 * 2 = 4; 2-2 = 4; 2/2 = 4。我現在將進行修改。另外{}也不是文本格式。我根據文本構建它。 – Chris 2014-09-23 19:32:54

回答

2

首先,你應該解釋你做了什麼,發生了什麼,以及你希望發生 - 因爲這通常使得事情更容易爲 任何人讀你的問題。

但是,您在{if}的身體周圍遺漏{}。

例如它應該是

if (operat == '*') { 
    ansrlt = firstint * secint; 
    cout << firstint << " " << operat << " " << secint << " = " << ansrlt << endl; 
} 

(而擺脫cin.get()電話,除非你使用它們用於調試)

+0

請注意,使用實際的調試程序可能會在比問題輸入時間更短的時間內顯示出來。學習如何使用你的工具,你會成爲一個快樂的開發者。 – 2014-09-23 19:24:43