2013-10-25 20 views
-2

以下程序在計算選擇後終止,並且沒有進一步發生。所以我的問題是我在做什麼錯?請有人檢查這個程序,並通知我我的錯誤。這個程序是一個計算器,它要求用戶選擇計算方法,即加法,除法或乘法等,然後顯示計算後的結果 Pic:http://i43.tinypic.com/2hykpjp.png 請原諒我,因爲我是C編程新手。這個程序有什麼問題,它終止?

main() 
{ 
    // declaration 
    int add,sub,mul,div,selection; 
    float a,b,c; 
    // prompt user to select a method 
    cout << "Calculator, which performs addition,subtraction,multiplication and division: add,sub,mul,div"; 
    cout << "Please enter your selection (for example: mul): "; 
    cin >> selection; 
    if (selection = add) 
    { 
      // prompt user to enter values 
      cout << "Please enter first value: "; 
      cin >> a; 
      cout << "Please enter second value: "; 
      cin >> b; 
      // calculations 
      c = a + b; 
      // result 
      cout << "Answer: " << c; 
    } 
    if (selection == sub) 
    { 
      // prompt user to enter values 
      cout << "Please enter first value: "; 
      cin >> a; 
      cout << "Please enter second value: "; 
      cin >> b; 
      // calculations 
      c = a - b; 
      // result 
      cout << "Answer: " << c; 
    } 
    if (selection == mul) 
    { 
      // prompt user to enter values 
      cout << "Please enter first value: "; 
      cin >> a; 
      cout << "Please enter second value: "; 
      cin >> b; 
      // calculations 
      c = a * b; 
      // result 
      cout << "Answer: " << c; 
    } 
    if (selection == div) 
    { 
      // prompt user to enter values 
      cout << "Please enter first value: "; 
      cin >> a; 
      cout << "Please enter second value: "; 
      cin >> b; 
      // calculations 
      c = a/b; 
      // result 
      cout << "Answer: " << c; 
    } 
} 
+0

這並不像C,但C++。 – Matthias

回答

1

基於您運行的程序的例子,你正在嘗試做的是輸入的字符串,如添加,然後對它們進行比較。你在程序中實際做的是:選擇,add,sub etcetera是整數變量,它只能用來存儲整數。

您必須聲明選擇作爲一個字符串變量,並將其值與字符串常量,像這樣:

string selection; 

然後:

if (selection == "add") 
+0

好的人,所以實際上錯誤是在我的/ /聲明的一部分。我宣佈了錯誤的變數。現在我正在使用 #include main() { //聲明 int x,y,z,d,e; x = 1; y = 2; d = 3; e = 4; float a,b,c; //我編的其餘部分 } 而我用x替換add,y用y替換,m用d和div替換e。也用'z'取代'選擇'。這一次,它變得狡猾。 :-) –

2

您還沒有初始化任何東西在這裏:

int add,sub,mul,div,selection; 

你拿值使用cin >> selection;selection但什麼是這些東西應該是指:

if (selection = add) 
if (selection == sub) 
if (selection == mul) 
if (selection == div) 

變量與存儲類auto未初始化爲任何默認值。

1

在下面的行中沒有初始化。

int add,sub,mul,div,selection; 

用獨特的值初始化這些變量(比如添加0,添加1)。 另外,在比較

selection = add 

使用

selection == add 

使用else if而不是分開IFS。它會提高性能。在你的情況下,它會比較每個條件。這只是一個例子。

if (selection = add) 
    { 
       // prompt user to enter values 
       cout << "Please enter first value: "; 
       cin >> a; 
       cout << "Please enter second value: "; 
       cin >> b; 
       // calculations 
       c = a + b; 
       // result 
       cout << "Answer: " << c; 
    } 
    else if (selection == sub) 
    { 
       // prompt user to enter values 
       cout << "Please enter first value: "; 
       cin >> a; 
       cout << "Please enter second value: "; 
       cin >> b; 
       // calculations 
       c = a - b; 
       // result 
       cout << "Answer: " << c; 
    } 

//Rest of your program.