#include #include //for the iomanipulator setw #include #include //for class string using namespace std; //sp (the "stack pointer") is the number of people currently employed. //For example, if sp is 3, then their names are in a[0], a[1], and a[2]. //If the company has any employees at all, a[sp-1] is always is the name of the //most recently hired one. int main() { int n {100}; //maximum number of people you can employ simultaneously string a[n]; //The stack is stored in an array of n strings. int sp {0}; //the "stack pointer": subscript of 1st unoccupied element cout << "Type a name (one word) to hire a person.\n" << "Type fire to fire the most recently hired person.\n" << "Type list to print the stack.\n" << "Type exit to exit the program.\n"; for (;;) { //Infinite loop, until we break out below. cout << "\nGo ahead: "; string word; cin >> word; if (word == "exit") { cout << "Goodbye.\n"; break; //out of the for loop. } else if (word == "list") { cout << "The " << sp << " current employees are:\n"; for (int i {0}; i < sp; ++i) { cout << setw(3) << i+1 << " " << a[i]; if (sp > 1) { //if more than 1 employee if (i == 0) { cout << " (oldest)"; } else if (i == sp-1) { cout << " (newest)"; } } cout << "\n"; } } else if (word == "fire") { if (sp == 0) { cerr << "Sorry, the stack is already empty.\n"; } else { --sp; //Begin by making the stack smaller. cout << "Firing " << a[sp] << " as employee #" << sp+1 << ".\n"; } } else { //If none of the above, assume the word is a name. if (sp == n) { cerr << "Sorry, the stack aleady contains " << sp << " people.\n"; } else { cout << "Hiring " << word << " as employee #" << sp+1 << ".\n"; a[sp] = word; ++sp; //End by making the stack bigger. } } } return EXIT_SUCCESS; //Arrive here when the user types exit. }