Why does this code cause my program to stop working?

#include <iostream>
#include <vector>
using namespace std;

int main()
{
cout << "\t\t\tWelcome to Vector Testing!\n\n\n";
vector<string>inventory;
inventory.push_back("sword");
inventory.push_back("armor");
inventory.push_back("shield");

vector<string>::iterator myIterator;
vector<string>::const_iterator iter;

cout << "Your items are:\n";
for(iter=inventory.begin();iter!=inventory.end(); ++iter)
{
cout << *iter << endl;
}
cout << "\nYou trade your sword for a battle axe.";
myIterator=inventory.begin();
*myIterator="battle axe";
cout << "\nYour items:\n";
for(iter=inventory.begin();iter!=inventory.end();++iter)
{
cout << *iter << endl;
}
cout << "\nThe item name '" << *myIterator << "' has ";
cout << (*myIterator).size() << "letters in it.\n";
cout <<"\nThe item name '" << *myIterator << "' has ";
cout << myIterator->size() << " letters in it.\n";

cout << "You recover a crossbow from a slain enemy.\n";
inventory.insert(inventory.begin(), "crossbow");
cout << "\nYour items are: \n";
for(iter=inventory.begin(); iter!=inventory.end(); ++iter);
{
cout << *iter << endl;
}
cout << "\nYour armor is destroyed in a fierce battle.";
inventory.erase((inventory.begin()+2));
cout << "\nYour items are:\n";
for(iter=inventory.begin(); iter!=inventory.end(); ++iter)
{
cout << *iter << endl;
}
return 0;
}

I dont understand whats wrong with it....
First and foremost don't use

using namespace std;

Second, you have not included your string.

#include <string>
you should limit the scope of your variables
37
38
39
40
for(iter=inventory.begin(); iter!=inventory.end(); ++iter); //note the semicolon
{
   cout << *iter << endl; //iter is iventory.end() here
}
for (auto s : inventory)
{
cout << s << endl;
}
@Militie, I just added the #include string. I had it up there before, but deleted it idk why. Still is doing the same thing though when I added the include string. Also whats wrong with using namespace std? That's how ive been taught :/


@ne555, thanks! I honestly thought it was my program being bad. I was looking through it for about 30 min, didnt even see the semicolon. It works now though :)
I don't usually nazi about not using namespace std;, but I like to not use it because it clearly shows what, and what is not part of the standard library.

But when you start using <algorithm> never use using namespace std;.
Look at poteto, he is right.

And about the #include, stuff like that happens all the time don't sweat it.
Im actually learning that part right now. In this book, the first thing he says is, "In order to use the STL algorithms, I include the file with their definitions. #include <algorithm> As you know, all STL components live in the std namespace. By using the following code (as i typically do), I can refer to algorithms without having to precede them with std::. using namespace std;" I've been using them in my last book, and just found out about std:: when i got this book. Although, this one doesnt use it. Just like he said, he uses 'using namespace std;'. So i dont know where to put std::. If there is a book that teaches the std:: way, ill get it.
> If there is a book that teaches the std:: way, ill get it.

C++ Primer (5th edition) - Lippman, Lajoie, Moo
http://www.amazon.com/Primer-5th-Edition-Stanley-Lippman/dp/0321714113

Accelerated C++ - Koenig, Moo
http://www.amazon.com/dp/020170353X
Topic archived. No new replies allowed.