首页 > 试题广场 >

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

[问答题]
编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。该程序的运行情况如下:
Please enter one of the following choice:
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
A maple is a tree.
Answer 1:

#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')
	{
        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;
}

Answer 2:(without switch)

#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')
	{
		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 : " ;
        	cin.get(ch);
	}
	return 0;
}

发表于 2022-03-06 14:33:44 回复(0)
#include <iostream>
using namespace std;
void showmenu();
int main()
{    
    showmenu();
    char ch;
    while (cin >> ch)
    {
        switch (ch)
        {
        case 'c' : cout << "A maple is a carnivore. \n";
            break;
        case 'p' : cout << "A maple is a pianist. \n";
            break;
        case 't' : cout << "A maple is a tree. \n";
            break;
        case 'g' : cout << "A maple is a game. \n";
            break;
        default  : cout << "Please enter a c, p, t, or g: \n";
          } 
    }
    return 0;
}

void showmenu()
{
    cout << "Please enter one of the following choices: \n"
            "c) carnivore  p) pianist\n"
            "t)tree        g) game\n";
}

发表于 2020-05-14 14:41:29 回复(0)
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
    cout << "Please enter one of the following choices:\n";
    cout << "c) carnivore" << "     " << "p) pianist\n";
    cout << "t) tree" << "        " << "g) game\n";
    char ch;
    cin.get(ch);
    while (isalpha(ch))
    {
        switch (ch)
        {
        case 'c':cout << "A maple is a carnivore.\n";
            break;
        case 'p':cout << "A maple is a pianist.\n";
            break;
        case 't':cout << "A maple is a tree.\n";
            break;
        case 'g':cout << "A maple is a game.\n";
            break;
        }
        cout << "Please enter a c,p,t,or g: ";
        cin >> ch;
        cin.get();
    }
    return 0;
}




发表于 2020-02-19 10:29:11 回复(0)