使用switch实现多选

代码:

// File name:           Multi_select_Switch
// Last modified Date:  2021年10月29日10点42分
// Last Version:        V1.0
// Descriptions:        使用switch语句实现多选

#include <iostream>
using namespace std;

void showmenu(); // function prototypes
void report();
void comfort();

int main()
{
	char choice;
	showmenu();
	cin >> choice;
	while (choice != 'Q' && choice != 'q')
	{
		switch (choice)
		{
		case 'a':
		case 'A': cout << "\a\n";
			break;
		case 'r':
		case 'R': report();
			break;
		case 'l':
		case 'L': cout << "The boss was in all day.\n";
			break;
		case 'c':
		case 'C': comfort();
			break;
		default: cout << "That's not a choice.\n";
		}
		showmenu();
		cin >> choice;
	}
	return 0;
}

void showmenu()
{
	cout << "Please enter A(OR a), R(OR r), L(OR l), C(OR c), or Q(OR q):\n"
		"A(OR a)) o newline R(OR r) report\n"
		"L(OR l) a notice C(OR c) comfort\n"
		"Q(OR q) quit\n";
}
void report()
{
	cout << "It's been an excellent week for business.\n"
		"Sales are up 120%. Expenses are down 35%.\n";
}
void comfort()
{
	cout << "Your employees think you are the finest CEO\n"
		"in the industry. The board of directors think\n"
		"you are the finest CEO in the industry.\n";
}

运行结果:

Please enter A(OR a), R(OR r), L(OR l), C(OR c), or Q(OR q):
A(OR a)) o newline R(OR r) report
L(OR l) a notice C(OR c) comfort
Q(OR q) quit
a

Please enter A(OR a), R(OR r), L(OR l), C(OR c), or Q(OR q):
A(OR a)) o newline R(OR r) report
L(OR l) a notice C(OR c) comfort
Q(OR q) quit
A

Please enter A(OR a), R(OR r), L(OR l), C(OR c), or Q(OR q):
A(OR a)) o newline R(OR r) report
L(OR l) a notice C(OR c) comfort
Q(OR q) quit
R
It's been an excellent week for business.
Sales are up 120%. Expenses are down 35%.
Please enter A(OR a), R(OR r), L(OR l), C(OR c), or Q(OR q):
A(OR a)) o newline R(OR r) report
L(OR l) a notice C(OR c) comfort
Q(OR q) quit
r
It's been an excellent week for business.
Sales are up 120%. Expenses are down 35%.
Please enter A(OR a), R(OR r), L(OR l), C(OR c), or Q(OR q):
A(OR a)) o newline R(OR r) report
L(OR l) a notice C(OR c) comfort
Q(OR q) quit
l
The boss was in all day.
Please enter A(OR a), R(OR r), L(OR l), C(OR c), or Q(OR q):
A(OR a)) o newline R(OR r) report
L(OR l) a notice C(OR c) comfort
Q(OR q) quit
L
The boss was in all day.
Please enter A(OR a), R(OR r), L(OR l), C(OR c), or Q(OR q):
A(OR a)) o newline R(OR r) report
L(OR l) a notice C(OR c) comfort
Q(OR q) quit
C
Your employees think you are the finest CEO
in the industry. The board of directors think
you are the finest CEO in the industry.
Please enter A(OR a), R(OR r), L(OR l), C(OR c), or Q(OR q):
A(OR a)) o newline R(OR r) report
L(OR l) a notice C(OR c) comfort
Q(OR q) quit
c
Your employees think you are the finest CEO
in the industry. The board of directors think
you are the finest CEO in the industry.
Please enter A(OR a), R(OR r), L(OR l), C(OR c), or Q(OR q):
A(OR a)) o newline R(OR r) report
L(OR l) a notice C(OR c) comfort
Q(OR q) quit
Q

D:\Prj\C++\C++_Learning\Multi_select_Switch\Debug\Multi_select_Switch.exe (进程 760)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .

上一篇:rsync远程同步


下一篇:自定义属性data-xx赋值并读取内容