Skip to content

Instantly share code, notes, and snippets.

@brianteachman
Last active August 12, 2016 19:04
Show Gist options
  • Save brianteachman/97eb057fe9971ffa6f02a5178f590e1e to your computer and use it in GitHub Desktop.
Save brianteachman/97eb057fe9971ffa6f02a5178f590e1e to your computer and use it in GitHub Desktop.
/*
* CH.8 Vectors: Class Exercize
*
* Brian Teachman & Brandon Sobjack
* 8/11/2016
*/
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void dumpVector(vector<string> list);
// Displays vector formated list to cout
void listCharacters(vector<string> list);
// Displays a numbered list to cout
string serveCharacter(vector<string> &list);
// Precondition: There is a character in the list
// Postcondition: 1) The first character in the queue (vector list) is removed from list.
// 2) All elements in the queue (vector list) shift left.
// 3) The name of the removed character is returned.
void runMenu(vector<string> &list);
// Displays runtime options menu
int main()
{
vector<string> characters = { "Luke" , "Leia", "Han" };
runMenu(characters);
return 0;
}
string serveCharacter(vector<string> &list)
{
string name = list[ 0 ];
list.erase( list.begin() );
return name;
}
void dumpVector(vector<string> list)
{
cout << "{ ";
for (int i = 0; i < list.size(); i++)
{
cout << list[i];
if (i != list.size() - 1) cout << ", ";
}
cout << " }" << endl;
}
void listCharacters(vector<string> list)
{
for (int i = 0; i < list.size(); i++)
{
cout << i + 1 << ": " << list[i] << endl;
}
}
void runMenu(vector<string> &list)
{
bool done = false;
while (!done)
{
char selection;
cout << "Choose an option: (L)ist, (S)erve, (A)dd, (R)emove, (Q)uit ";
cin >> selection;
string new_name;
int remove_name;
switch (selection)
{
case 'L': case 'l':
listCharacters(list);
break;
case 'S': case 's':
cout << serveCharacter(list) << " has been served." << endl;
dumpVector(list);
break;
case 'A': case 'a':
cout << "Enter a name to add to the character list: " << endl;
cin >> new_name;
list.push_back(new_name);
dumpVector(list);
break;
case 'R': case 'r':
listCharacters(list);
cout << "Select a character by it's number: ";
cin >> remove_name;
list.erase(list.begin() + (remove_name - 1));
break;
case 'Q': case 'q':
exit(1);
break;
default:
runMenu(list);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment