1

Currently learning C++ building a little program that takes in movie names, stores them in a vector and then outputs it based on user input. The problem is I made a function for displaying the movies list. Now every time I run that function I get this error:

Unhandled exception at 0x769DA842 in ConsoleApplication2.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0113F674.

Here's the code:

#include <iostream>
#include <vector>

std::vector <std::string> movieList;
std::vector <int>::iterator it;

void AddMovie(std::string movie)
{
    movieList.push_back(movie);
}

void DeleteMovie(int i)
{
    movieList.erase(movieList.begin() + i - 1);
}

void ShowMovies()
{
    for (int i = 0; movieList.size() > 1; i++)
    {
        std::cout << movieList.at(i) << std::endl;
    }
}

int main()
{

    int i{ 0 };

    movieList.push_back("Inception");
    movieList.push_back("Peter Pan");
    movieList.push_back("Jaws");

    std::cout << "Delete movie: ";
    std::cin >> i;
    DeleteMovie(i);
    ShowMovies();

    
}

The error breaks at line

std::cout << movieList.at(i) << std::endl;

3
  • 5
    for (int i = 0; movieList.size() > 1; i++) will go on forever if there are at least 2 items in the list. You probably meant for (int i = 0; i < movieList.size(); i++) Commented Feb 9, 2021 at 18:56
  • Does this answer your question? What is a debugger and how can it help me diagnose problems? Commented Feb 9, 2021 at 19:01
  • Very helpful! Thank you for correcting my logic :) Commented Feb 9, 2021 at 19:10

1 Answer 1

1

In the for loop, the condition movieList.size() > 1 is always true (if you have more than one movie in the list), so you are incrementing i beyond the size of the list, accessing to memory out of the range of the vector.

This example should work, since the variable i is only used in the body of the loop till it reach movieList.size() - 1 (as @user4581301 commented above, it is increased to movieList.size(), but that last value is not used in the loop body):

void ShowMovies()
{
    for (int i = 0; i < movieList.size(); i++)
    {
        std::cout << movieList.at(i) << std::endl;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Alternative: for (const auto & val: movieList) {std::cout << val << std::endl; } Documentation on range-based for
Minor correction: the variable i is only incremented till it reach movieList.size() - 1: is not quite true. i is incremented to movieList.size(), but i reaching movieList.size() is the exit condition so movieList.size() - 1 is not used in the body of the loop. i goes out of scope immediately after reaching movieList.size() so the point is moot.
@user4581301 you are right, it is incremented once more than I said, but not used in the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.