if else string & vector

1
2
3
4
5
6
7
8
9
10
11
vector<Manager> Managers;   
string name;
cout << "\tEnter new manager name to add: ";
cin >> name;
for(int i=0; Managers.size(); i++){
    if(name == Managers[i].getName()){
    //do nothing
    }
    else{
        Managers.push_back(Manager(name));
    }


After input name, program stopped working. Can anyone tell me what's the reason and how to fix it?
closed account (EwCjE3v7)
Look at line 5, for(int i=0; Managers.size(); i++). What does Managers.size(); do? It gets the size of Managers and dosnt do anything with it.

So this loop will go forever, it will never end. You need to add this, i != Managers.size()

So line 5 will look like for(int i=0; i != Managers.size(); i++){.

And may I make a few other points. Try to avoid mixing singed and unsigned. Above, the int is signed and Managers.size() is unsigned. The type returned by Managers.size() is std::vector::size_type. So maybe you would want to do this:
for(vector::size_type i = 0; i != Managers.size(); i++){
Last edited on
Xp3rtHamm0r: Why use string::size_type? vector<Manager>::size_type makes more sense. Personally I think it get's a bit verbose so I normally use std::size_t instead.
closed account (EwCjE3v7)
Oh I edited my previous post. Thanks for letting me know. Thought Managers was a string ;).

Thank you
Topic archived. No new replies allowed.