# Vector problem.

This is a discussion on Vector problem. within the C Programming forums, part of the General Programming Boards category; Code: int main() { char list[100]; char list1[100]; vector<string> gList; vector<string>::iterator game; vector<string>::const_iterator myGame; cout << "Please insert a game ...

1. ## Vector problem.

Code:
```int main()
{
char list[100];
char list1[100];
vector<string> gList;
vector<string>::iterator game;
vector<string>::const_iterator myGame;

cout << "Please insert a game you would like to add: ";
cin.getline(list, 100);
gList.push_back(list);
cout << "Please insert another game: ";
cin.getline(list1, 100);
gList.push_back(list1);

cout << "you got " << gList.size() << " games.";
for(game = gList.begin(); game != gList.end(); ++game)
cout << "\nAnd they are: " << *game;
getch();
system("cls");
char choice[100];
cout << "Would you like to remove " << list << " or " << list1 << "?";
cin.getline(choice, 100);
if(choice == list)
{
gList.erase((gList.begin(), +0));
}
else if(choice == list1)
{
const string* str_int = gList;    //I want to make this part an int,
gList.erase((gList.begin(), +1)); //but how do I do that?.
}
cout << "You now got " << gList.size() << " game left.";
for(myGame = gList.begin(); myGame != gList.end(); --myGame)
cout << "\nAnd it is " << *myGame;
getch();
return 0;
}```

2. That's some interesting C, right there. But anyway, what you want makes no sense. How can you turn a vector of strings into an int?

(Not that this line could ever happen, since choice == list2 must always evaluate false, since you are comparing the addresses of the arrays.)

3. And it should be const char* str_int = whatever

just tried different solutions

4. well, I dont know if the +1 work like when I push_back a string directly to the gList, just trying my way through.
The compiler is complaining(like always!!) that I cant convert std::vector<string> etc etc.

5. Neither I nor the compiler seem to be getting through to you:
gList is something like <"george", "bob", "larry", "larry 2", "larry 3">. Why do you want, or expect, to turn this into an int?

6. hehe im so sorry tabstop. ^^

maybe im reading the error message wrong, it says that
erase(vector<..>::iterator) cannot convert parameter 1 from int to vector<..>::iterator with type string.

7. Right. Because +1 is an int, and shouldn't be. Look at erase again:
Originally Posted by cplusplus.com
iterator erase ( iterator first, iterator last );
You need to pass an iterator of where to start, and an iterator one past the thing you want to erase.
OR, you could use the one-parameter version:
Originally Posted by cplusplus.com
iterator erase ( iterator position );
which erases ONE thing where the iterator points.

(Note also you can add to iterators, so if you want to point to the second thing of a vector you can do gList.begin()+1. Note the absence of a comma.)

8. and ye, I posted in the wrong group ^^

9. well I can just type
Code:
`gList.erase((gList.end()));`
except that is not erasing anything right now ^^

10. Well, I would hope not. end() does not point at any valid element of a vector. If you want to erase the first element, you would use begin(). If you want to erase the second element, you would use begin()+1. end() is never something you can erase.

11. well ive tried the +1 thingy, that is the problem I had when I posted this

12. Originally Posted by And3rs
well ive tried the +1 thingy, that is the problem I had when I posted this
There is still no comma in "+1".

Edit: In other words:
Code:
```erase(gList.begin()+1); //as beautiful as ... well, let's just say "beautiful"
erase(gList.begin(), +1); //has no chance on this earth of ever meaning anything```

13. Im sorry if I seems retarted or something, but I still need a comma after
gList.erase((gList.begin(),+1))) ???

14. Originally Posted by And3rs
Im sorry if I seems retarted or something, but I still need a comma after
gList.erase((gList.begin(),+1))) ???
There should not be a comma anywhere in that line. Not not not. (When erasing a single thing, erase only takes one argument, not two.)

15. well it&#180;s complaining about that it is missing one ;

Page 1 of 2 12 Last