以下程序在計算選擇後終止,並且沒有進一步發生。所以我的問題是我在做什麼錯?請有人檢查這個程序,並通知我我的錯誤。這個程序是一個計算器,它要求用戶選擇計算方法,即加法,除法或乘法等,然後顯示計算後的結果 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;
}
}
這並不像C,但C++。 – Matthias