Can someone tell me why I get this error
"terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
Abort trap"

when i run the following code? user and reservations are vectors of two classes

        else if (str == "lookup")
            getline(cin, Name, '\n');
            if (Name == user.at(i).getName()) 
                for (int i = 0; i < user.size() ; i++)
                    cout << user.at(i).getName();
                    cout << user.at(i).getphone();
                    cout << reservations.at(i).getflight();
                    cout << user.at(i).getdate();

5 Years
Discussion Span
Last Post by deceptikon

By using the at() member function, out of range indices will throw std::out_of_range. So I'd wager i is out of range for the reservations vector given that i is bound to the size of the user vector. Though you've also got some fun looking scope hiding going on with i that may be doing unexpected things.

Edited by deceptikon


I figured it was that but when i change user.size() in the for loop to a constant variable it still gave me the same error


when i change user.size() in the for loop to a constant variable

Unrelated things are unrelated. The loop is bound to the size of user, but you assume (apparently incorrectly) that reservations is large enough to use any index valid for user.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.