#include <iostream> #include <cstdlib> #include <string> //for class string using namespace std; //Definitions of global variables. const int n {100}; //maximum number of people you can employ simultaneously string a[n]; //Store the stack in an array of n strings. int sp {0}; //the "stack pointer": subscript of 1st unoccupied element //Declarations of functions. void push(string name); string pop(); //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() { cout << "Type a name (one word) to hire a person.\n" << "Type fire to fire the most recently hired person.\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 == "fire") { word = pop(); cout << "Firing " << word << ".\n"; } else { //If none of the above, assume the word is a name. push(word); cout << "Hiring " << word << ".\n"; } } return EXIT_SUCCESS; //Arrive here when the user types exit. } void push(string name) { if (sp == n) { cerr << "Sorry, stack already contains " << sp << " people.\n"; exit(EXIT_FAILURE); } else { a[sp] = name; ++sp; } } string pop() { if (sp == 0) { cerr << "Sorry, the stack is already empty.\n"; exit(EXIT_FAILURE); } else { --sp; return a[sp]; } }