#include <iostream> //include header files
using namespace std;
int main() //start of main body
{
int num; //declaring integer
int control=1; //declaring integer
while(control==1)//start of loop, as long as condition is true
{
cout << "Press 1 for coffee" << endl; //writes to standard o/p
cout << "Press 2 for tea" << endl;
cout << "Press 3 for hot chocolate" << endl;
cout << "Press 4 for soft drink" << endl;
cout << "Press 5 to exit" << endl;
cin >> num;
if (num == 1) //code to execute if integer = 1
{
cout << "coffee selected" << endl;
}
else if (num == 2) //code to execute integer = 2
{
cout << "tea selected" << endl;
}
else if (num == 3) //code to execute if integer = 3
{
cout << "hot chocolate selected" << endl;
}
else if (num == 4) //code to execute if integer = 4
{
cout << "soft drink selected" << endl;
}
else if (num == 5) //code to execute if integer = 5
{
cout << "Exit Program" << endl;
control=0;
}
}
是我ammended代碼這個工程。然而,我不確定初始化數字整數,所以我離開它,但代碼仍然執行並正常工作。
'如果(NUM == 1)',** **不'如果(NUM == 「1」)'。 – us2012 2013-03-24 17:10:50
(歡迎使用stackoverflow。請閱讀常見問題解答,並且爲了將來的問題,請記住,如果您(1)做出一個簡短而簡明的例子來展示您的意思,那麼人們將更有可能幫助您 - 您可以削減80%的代碼,仍然顯示問題,並且(2)提供準確的錯誤信息和行號,並在代碼中標記相關行,以便我們不必逐行閱讀。) – us2012 2013-03-24 17:12:46
也可能想要將某些內容實際分配給num '在某個時候。 – JasonD 2013-03-24 17:13:27