Hello everyone, I'm currently working with exercises in C++ with vectors and iterators to go with them as well. This program I've written works well, only things I have trouble with is for the user input with vectors. Like if I use getline(cin, string); without vectors I can get the whole game title fine but its not working here.
So instead i'm just using this and it can one get a string without any spaces :
1 2 3 4 5 6 7 8 9 10
case ADD:
cout << "\nName of title to add:\n";
cin >> gameToAdd;
if (!gameToAdd.empty()) {
favoriteGameTitles.push_back(gameToAdd);
cout << "\nGame title successfully added!\n";
} else
cout << "\nInvalid game title\n";
break;
And if anyone can point to me any mistakes that should not be in here please do tell, I want to make the most improvements as I can. Thank you.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
// Holds the current states for the options
enum states { NONE, LIST, ADD, REMOVE, QUIT };
// Holds the player option input
int option = NONE;
// Iterators to iterate through the game titles
vector<string>::iterator myIterator;
vector<string>::const_iterator iter;
// A collection of strings that holds the user's game titles
vector<string> favoriteGameTitles;
// Option menu
cout << "Welcome to your personal favorite game titles!\n\n";
cout << "1.List your current game titles\n";
cout << "2.Add a game title\n";
cout << "3.Remove last game title added\n";
cout << "4.Quit program\n";
// Enter the program loop
while (option != QUIT)
{
// Get the players option input
cout << "\nOption: ";
cin >> option;
string gameToAdd;
switch (option)
{
case LIST:
cout << "\nYour current game titles:\n";
if (favoriteGameTitles.empty()) // player has no game titles added so display empty message
cout << "You have no titles added.\n";
elsefor (iter = favoriteGameTitles.begin(); iter != favoriteGameTitles.end(); ++iter) // print the player's game titles
cout << *iter << endl;
break;
case ADD:
cout << "\nName of title to add:\n";
cin >> gameToAdd;
if (!gameToAdd.empty()) {
favoriteGameTitles.push_back(gameToAdd);
cout << "\nGame title successfully added!\n";
} else
cout << "\nInvalid game title\n";
break;
case REMOVE:
if (!favoriteGameTitles.empty()) {
favoriteGameTitles.pop_back();
cout << "\nGame title successfully removed!\n";
} else
cout << "\nYou have no game titles to remove!\n";
break;
case QUIT: // exits main program loop
cout << "\nThanks for using this program!\n";
break;
default: cout << "\nInvalid option.\n";
}
}
return 0;
}
Also should I be using maps instead of vectors? I want the user to be able to take out a title from maybe the middle of the collection and not just from the bottom of the stack everytime with pop_back();
> Like if I use getline(cin, string); without vectors I can get the whole game title fine
you can use getline() with vectors too.
1 2 3 4 5 6 7 8 9 10 11
case ADD:
cout << "\nName of title to add:\n";
// cin >> gameToAdd;
std::getline( std::cin, gameToAdd ) ;
if (!gameToAdd.empty()) {
favoriteGameTitles.push_back(gameToAdd);
cout << "\nGame title successfully added!\n";
} else
cout << "\nInvalid game title\n";
break;
> Also should I be using maps instead of vectors?
A std::set<>, you don't need a std::map<> unless you are associating some data with the key (the game title). It would be more time efficient if there are a large number of game titles.
Do it any way, have two versions of your program; it is good experience.
Thanks so much for helping me out here! I really do appreciate everyone that does help me. And by the way what do you mean two versions? Save this first version, and make a second one? =] and what is a key :/
EDIT: getline() function didn't work. Right when I entered the option 2, to enter a game title. It's output was :
1 2 3 4 5
Option : 2
Name of title to add:
Invalid game title
Option :
Thank you again! And why did you put : cin.ignore(1024*1024);
It didn't work lol, but when I made it to : cin.ignore(1);
It worked perfect, maybe you just put it there for me to figure out =] thanks though.