#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;
}
}
}
这是我修改后的代码,它有效。但是我不确定初始化 num 整数,所以我把它省略了,但代码仍然执行并正常工作。