首页 > 试题广场 >

编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单

[问答题]
编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单,每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单的操作。该程序的运行情况如下:
Please enter one of the following choices:
c) carnivore            p) pianist
t) tree                     g) game
f
Please enter a,c,p,t,or,g:q
Please enter a,c,p,t, or g:t
Amaple is  a tree.
#include<iostream>
int main()
{
	using namespace std;
	cout << "Please Enter one of the following choices: " << endl;
	cout << "c)carnivore\t p)pianist\t" << endl;
	cout << "t)tree\t g)game\t q)quit\t" << endl;
	char ch;
	while ((ch=cin.get())!='q')
	{
		/*another way to solve this problem without switch
        if (ch == 'c')
			cout << "Do you like carnivore? " << endl;
		else if (ch == 'p')
			cout << "There is a pianist " << endl;
		else if (ch == 't')
			cout << "A maple is a tree  " << endl;
		else if (ch == 'g')
			cout << "Hello! Come on and join us ! " << endl;
		else
			cout << "Please Enter a c,p,t,g : " ;*/
		switch (ch)
		{
			case 'c':cout << "Do you like carnivore? " << endl ; break;
			case 'p':cout << "There is a pianist " << endl; break;
			case 'g':cout << "Hello! Come on and join us ! " << endl; break;
			case 't':cout << "A maple is a tree " << endl; break;
			default:cout<< "Please Enter a c,p,t,g : " << endl;
		}
		cin.get(ch);
	}
	return 0;
}

发表于 2022-03-06 14:35:38 回复(0)