Help sorting an array inside a struct

Hello all I am trying to figure out how to sort an array(actors) by their last name using bubble sort.I have a function that gets the lastname of the actors for one movie, but when I try to print out all the movies I get the error.
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr


I can sort the struct on any field but the actors. I can sort by actors with only one movie. It is when I try to sort with all 6 movies that I can not figure out. Any tips on what I am doing wrong would be greatly appreciated.


#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct Movie
{
string name;
string year;
string directors;
string actors[5];
int count;


};

string lastname (string name)
{
int i = name.size() - 1;
while(name[i] != ' ') i--;
i++;
return name.substr(i,255);
}



void bubblesortnn( Movie movies[6], int N)
{
int i,swaps = 1;
while (swaps)
{
swaps = 0;
for (int i = 0; i < 6; i++)
{
movies[i];
for(int j=0;j<=4;j++)
{
if(movies[i].actors[j] > movies[i].actors[i+j])
{
swap(movies[i].actors[j], movies[i].actors[i+j]);
swaps = 1;
}
}
}

}
}






void bubblesort( Movie movies[6], int N)
{
int i,swaps = 1;
while (swaps)
{
swaps = 0;
for (i = 0; i < N-1; i++)
if (movies[i].name > movies[i + 1].name)
{
swap(movies[i], movies[i+1]);
swaps = 1;
}
}
}




struct Movie movies[6];
int main()
{
int N = 6;

string st;
int i = 0;
int j = 0;
getline(cin, movies[i].name);
getline(cin, movies[i].year);
getline(cin, movies[i].directors);

while(getline(cin,st))
{
if(st[0] != '=')
{
movies[i].actors[j] = st;
j++;
}
else
{
movies[i].count = j - 1;
i++;

getline(cin,movies[i].name);
getline(cin,movies[i].year);
getline(cin,movies[i].directors);

j = 0;

}


}
cout<<endl;
for(int i = 0; i<=5;i++)
{
cout<<movies[i].name<<endl;
cout<<movies[i].year<<endl;
cout<<movies[i].directors<<endl;
for (int j = 0; j <=4; j++)
{
cout<<lastname(movies[i].actors[j])<<endl;
}

}


/*
bubblesortnn(movies,N);
for(int i =0; i<=5;i++)
{
cout<<movies[i].name<<endl;
cout<<movies[i].year<<endl;
cout<<movies[i].directors<<endl;

for(int j=0; j<=4;j++)
{
cout<<movies[i].actors[j]<<endl;
}
}



*/

}

Heres the data I am using

Snow White and the 7 Dwarfs
1937
Director: David Hand
Adriana Caselotti
Lucille La Verne
Harry Stockwell
====================
Pinocchio
1940
Directors: Ben Sharpsteen Hamilton Luske
Dickie Jones
Cliff Edwards
Mel Blanc
====================
Cinderella
1950
Directors: Clyde Geronimi Hamilton Luske Wilfred Jackson
Ilene Woods
Eleanor Audley
Verna Felton
James MacDonald
Betty Lou Gerson
====================
Peter Pan
1953
Directors: Clyde Geronimi Wilfred Jackson Hamilton Luske
Bobby Driscoll
Kathryn Beaumont
Hans Conried
Tommy Luske
====================
Lady and the Tramp
1955
Directors: Clyde Geronimi Wilfred Jackson Hamilton Luske
Peggy Lee
Barbara Luddy
Larry Roberts
====================
The Rescuers
1977
Directors: John Lounsbery Wolfgang Reitherman Art Stevens
Bob Newhart
Eva Gabor
Geraldine Page
Joe Flynn

closed account (D80DSL3A)
I think your problem is within the bubblesortnn function: movies[i].actors[i+j]
where i+j goes as high as 9 but the size of the array actors[] is 5. It should be j+1 instead and with the range restricted to 0 through size-2.
Something like this (untested):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void bubblesortnn( Movie movies[], int N)
{
for (int i = 0; i < N; i++)// for each movie
{
    int swaps = 1;
    while (swaps)// swap the actors
    {
        swaps = 0;
        for(int j=0;j<4;j++)// not <=
        {
            if(movies[i].actors[j] > movies[i].actors[j+1])// j+1 not i+j
            {
                swap(movies[i].actors[j], movies[i].actors[j+1]);
               swaps = 1;
            }
       }
    }
}
}
It works! Thank you I have spent hours trying to figure that out!
Now I am trying to figure out how to get it to sort by the actor's last name. My function works but only one movie at a time. For some reason it cannot find the second movie.
Here is what it does:
Snow White and the 7 Dwarfs
1937
Director: David Hand

1Adriana Caselotti
1i16
2i7
2Adriana Caselotti
3i8
Caselotti
1Lucille La Verne
1i15
2i10
2Lucille La Verne
3i11
Verne
1Harry Stockwell
1i14
2i5
2Harry Stockwell
3i6
Stockwell
1
1i-1
2i-1848
2
3i-1847

All I am trying to do now is print out the movies with only the last name for the actors' names. Her is the code i am using
for(int i = 0; i<=5;i++)
{
cout<<movies[i].name<<endl;
cout<<movies[i].year<<endl;
cout<<movies[i].directors<<endl;
for (int j = 0; j <=4; j++)
{
cout<<lastname(movies[i].actors[j])<<endl;
}

}
Topic archived. No new replies allowed.