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. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    65

    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;
    }
    Last edited by And3rs; 10-14-2008 at 04:49 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    And it should be const char* str_int = whatever

    just tried different solutions

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    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.
    Last edited by And3rs; 10-14-2008 at 04:22 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    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.

    My bad again

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Right. Because +1 is an int, and shouldn't be. Look at erase again:
    Quote 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:
    Quote 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. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    and ye, I posted in the wrong group ^^

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    well I can just type
    Code:
    gList.erase((gList.end()));
    except that is not erasing anything right now ^^

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    well ive tried the +1 thingy, that is the problem I had when I posted this

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by And3rs View Post
    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. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    Im sorry if I seems retarted or something, but I still need a comma after
    gList.erase((gList.begin(),+1))) ???

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by And3rs View Post
    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. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    well it&#180;s complaining about that it is missing one ;

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21