C++ primer plus第六版课后编程练习答案:6.4

#define  _CRT_SECURE_NO_WARNINGS 
#include 
#include
#include
using namespace std;

const int strsize = 30;
//结构描述
struct bop{
    char fullname[strsize];
    char title[strsize];
    char bopname[strsize];
    int preference;
};
int main()
{
    //创建结构变量
    bop Man1{
        "Wimp Macho",
        "Actor",
        "Skull reaper",
        0
    };
    bop Man2{
        "Raki Rhodes",
        "Junior Programmer",
        "Asker",
        1
    };
    bop Man3{
        "Celia Laiter",
        "Teacher",
        "MIPS",
        2
    };
    bop Man4{
        "Hoppy Hipman",
        "Analyst Trainee",
        "Answerer",
        1
    };
    bop Man5{
        "Pat Hand",
        "C programmer",
        "LOOPY",
        2
    };
    cout << "Benevolent Order of Programmers Report\n";
    cout << "a. display by name\t\tb. displaay by title\nc. display by bopname\t\td. display by preference\nq. quit\n";
    cout << "Enter your choice: ";
    char ch;
    ch = cin.get();
    while (true)//最外层循环,目的是为了使得错误输入提示并重新输入的功能,switch选择的功能能够在同一个循环体内进行
    {
        if (ch == 'q')//输入q退出
            break;
        while ((ch != 'a') && (ch != 'b') && (ch != 'c') && (ch != 'd') && (ch != 'q'))//提示错误输入,并重新输入
        {
            cout << "Please enter a b,c,d, or q: ";
            while (cin.get() != '\n')
                continue;
            ch = cin.get();
        }
        while (ch != 'q')
        {
            switch (ch)
            {
            case'a':{
                        cout << Man1.fullname << endl << Man2.fullname << endl << Man3.fullname << endl << Man4.fullname << endl << Man5.fullname << endl;
            }break;
            case'b':{
                        cout << Man1.title << endl << Man2.title << endl << Man3.title << endl << Man4.title << endl << Man5.title << endl;
            }break;
            case'c':{
                        cout << Man1.bopname << endl << Man2.bopname << endl << Man3.bopname << endl << Man4.bopname << endl << Man5.bopname << endl;
            }break;
            case'd':{
                        cout << Man1.fullname << endl << Man2.title << endl << Man3.bopname << endl << Man4.title << endl << Man5.bopname << endl;
            }break;
            default:
                break;
            }
            cout << "Next choice: ";
            cin.get();//消除换行符的影响
            ch = cin.get();
        }
    }
    cout << "Bye!" << endl;
    return 0;
}

你可能感兴趣的:(C++,primer,plus第六版课后编程练习)