I have to start over again every day, c++primer plus Chapter 6, Question 4, the use of the switch statement

# include<iostream>
# include<cstring>
using namespace std;
const int strsize = 100;
struct bop //define the structure
{
	char fullname[strsize];
	char title[strsize];
	char bopname[strsize];
	int preference;
};
intmain()
{
	char choice;
	bop persons[3] = { //Assign the structure variable
		{
			"a", "aa", "aaa", 0 //Can be changed to the value of the output result on the book
		},
		{
			"b", "bb", "bbb", 1
		},
		{
			"c", "cc", "ccc", 2
		},
	};
	cout << "Please enter a,b,c,d,q"; // can be changed to a large piece of output in the book
	while (cin >> choice) //while loop input
	{
		if (choice != 'q'&&choice != 'Q') //Judging whether it is q, terminate the program, if not, continue to the next step
		{
			int i;
			switch (choice) // judge and output
			{
			case 'a':
			case 'A': for (i = 0; i < 3; i++)
				cout << persons[i].fullname << endl;
				break;
			case 'b':
			case 'B': for (i = 0; i < 3; i++)
				cout << persons[i].title << endl;
				break;
			case 'c':
			case 'C': for (i = 0; i < 3; i++)
				cout << persons[i].bopname << endl;
				break;
			case 'd':
			case 'D': for (i = 0; i < 3; i++)
			{
						  switch (persons[i].preference)
						  {
						  case 0: cout << persons[i].fullname << endl;
							  break;
						  case 1: cout << persons[i].title << endl;
							  break;
						  case 2: cout << persons[i].bopname << endl;
							  break;
						  default:cout << "Please enter the correct value";
						  }
			}

			}
		}
		else break;
	}
	cout << "Bye!";
	system("pause");
	return 0;
}

If you have any questions, you can leave a message below, and I will answer them and learn from each other.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324821486&siteId=291194637