当前位置:C++技术网 > 精选软件 > C++ Primer Plus 6th 6.11 编程练习题 第3题 编写一个菜单驱动程序的雏形

C++ Primer Plus 6th 6.11 编程练习题 第3题 编写一个菜单驱动程序的雏形

更新时间:2019-05-10 15:47:21浏览次数:1+次

题目:

3.编写一个菜单驱动程序的雏形。该程序显示一个提供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
a maple is a tree.

答案:  书上无答案。


C++技术网辅导详解解答:
    参考代码:

#include <iostream>
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;
    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:";
        }
    }
    return 0;
}