ccpp5 編程練習6.4

此網站上有參考XD: 

http://www.ignatkov.net/cppprimerplus/ ios

5月3日查看,此網站已被黑。 網站

// ex6.4
// 但願回頭想到好辦法
#include <iostream>
using namespace std;
const int strsize = 20; // 20 或許有點小
const int NUM = 5;
void showmenu();
// Benevolent Order of Programmers name structure
struct bop {
	char fullname[strsize]; // real name
	char title[strsize];    // job title
	char bopname[strsize];  // secret BOP name
	int preference;         // 0 = fullname, 1 = title, 2 = bopname
};

int main ()
{
	// 除了書上貼出來的信息,其餘瞎編
	bop bops[NUM] = {
		{"Wimp Macho", "Farmer", "Wim", 0},
		{"Raki Rhodes", "Junior Programmer", "Rak", 1},
		{"Celia Laiter", "Driver", "MIPS", 2},
		{"Hoppy Hipman", "Analyst Trainee", "Hop", 1},
		{"Pat Hand", "Teacher", "LOOPY", 2}
	};

	showmenu();
	char ch; // choice
	cout << "Enter your choice: ";
	int i;   // 不重複聲明瞭

	// 這個寫的複雜了,但願能想出好辦法
	while (cin >> ch && ch != 'q')
	{
		switch (ch)
		{
			case 'a': for (i = 0; i < NUM; i++)
					  cout << bops[i].fullname << endl;
				  break;
			case 'b': for (i = 0; i < NUM; i++)
					  cout << bops[i].title << endl;
				  break;
			case 'c': for (i = 0; i < NUM; i++)
					  cout << bops[i].bopname << endl;
				  break;
			case 'd': for (i = 0; i < NUM; i++)
				  {
					  switch (bops[i].preference)
					  {
						  case 0: cout << bops[i].fullname << endl; break;
						  case 1: cout << bops[i].title << endl; break;
						  default: cout << bops[i].bopname << endl;
					  }
				  }
				  break;
			default: break;
		}
		// 吞掉後面多掛的字符
		while ((ch = cin.get()) != '\n')
			continue;
		cout << "Next choice: ";
	}
	cout << "Bye!\n";
	return 0;
}

void showmenu()
{
	cout << "Benevolent Order of Programmers Report\n"
		"a. display by name     b. display by title\n"
		"c. display by bopname  d. display by preference\n"
		"q. quit\n";
	return;
}
相關文章
相關標籤/搜索