1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main()
{
char StartOver;
do
{
cout << "*********************************************************************" << endl;
cout << "*********************************************************************" << endl;
cout << "*********************************************************************" << endl;
cout << "\n\n\t\t\t\tWelcome.\n\n\t\t Video game list." << endl;
vector<string> games; // create a vector variable of type string
games.push_back("Chrono Trigger"); // add to the string vector list
games.push_back("Metal Gear");
games.push_back("Fallout");
vector<string> :: iterator myGames; // iterator named my games
vector<string> :: const_iterator iter;// constant iterator named iter
unsigned short int i;
cout << "\n\nYour current list of games: \n" << endl;
for(iter = games.begin(), i = 1; iter != games.end(), i <= games.size(); ++iter, ++i) // List all the current games
{
cout << "\t" << i << ". " << *iter << endl; // list the number of game and the games
}
int elementPosition = 0; // So I can choose the element
do
{
cout << "\nType the NUMBER of the game you want to trade: ";
cin >> elementPosition; //user chooses element
cin.ignore(); // I think this prevents the compiler from ignoring content left in input buffer and skipping code
if(elementPosition > games.size() || elementPosition < 1)
{
cout << "\n\t**Invalid choice.** \nPick a number from the list of games.\n" << endl;
}
}while(elementPosition > games.size() || elementPosition < 1); // Don't go above or below the vector size
elementPosition -= 1; //This is to make sure a number below 1 is not chosen as an option
cout << "\nWhat game are you trading " << games[elementPosition] << " for?\n";
//elementPosition++; // increment element position in order to pick number 1 or higher on the list
string name; // The name of the game that will be traded
getline(cin, name); // Allows string to have more than one word
myGames = games.begin() + elementPosition; // first vector element is assigned to myGames + the game I want to trade
*myGames = name;
cout << "\n\nThe list has been updated to: \n" << endl;
for(iter = games.begin(), i = 1; iter != games.end(), i <= games.size(); ++iter, ++i) // List all the current games
{
cout << i << ". " << *iter << endl; // list the number of game and the games
}
cout << "\nWhat game would you like to add to the list? ";
getline(cin, name);
myGames = games.insert(games.end(), name); //Choose where to add new game. I chose at the end
cout << "\nThe list has been updated to: \n" << endl;
for(iter = games.begin(), i = 1; iter != games.end(), i <= games.size(); ++iter, ++i) // List all the current games
{
cout << i << ". " << *iter << endl; // list the number of game and the games
}
cout << "\nType the NUMBER of the game you getting rid of? "; // game to erase from list
int gameToErase;
cin >> gameToErase;
gameToErase -= 1; // compiler starts counting at 0, so 1 is actually the second element
games.erase((games.begin() + gameToErase));
cout << "\nThe list has been updated to: \n" << endl;
for(iter = games.begin(), i = 1; iter != games.end(), i <= games.size(); ++iter, ++i) // List all the current games
{
cout << i << ". " << *iter << endl; // list the number of game and the games
}
cout << "Do you wish to Start the list over? (y = yes, and n for no) ";
cin >> StartOver;
}while(StartOver == 'y'); // start the list over in yes is pressed
return 0;
}
|