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;

AdamKxZ
  • 35
  • 5
  • 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++)` – 001 Feb 09 '21 at 18:56
  • Does this answer your question? [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Aykhan Hagverdili Feb 09 '21 at 19:01
  • Very helpful! Thank you for correcting my logic :) – AdamKxZ Feb 09 '21 at 19:10

1 Answers1

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;
    }
}
  • 2
    Alternative: `for (const auto & val: movieList) {std::cout << val << std::endl; }` [Documentation on range-based `for`](https://en.cppreference.com/w/cpp/language/range-for) – user4581301 Feb 09 '21 at 19:04
  • 1
    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 Feb 09 '21 at 19:09
  • @user4581301 you are right, it is incremented once more than I said, but not used in the loop. – Alberto Casas Ortiz Feb 09 '21 at 19:13