#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct movie
{
string name;
string date;
string starx[10];
string star[10];
int count;
} film[10];
void sort_stars(string ar[], string arx[], int i)
{
int swaps = 1, x;
while(swaps)
{
swaps = 0;
for(x = 0; x < i - 1; x++)
{
if(arx[x].compare(arx[x+1]) == 1)
{
swap(arx[x], arx[x+1]);
swap(ar[x], ar[x+1]);
swaps = 1;
}
}
}
}
void sort_movie(struct movie film[], int x)
{
int swaps = 1, a;
while(swaps)
{
swaps = 0;
for(a = 0; a < x - 1; a++)
{
if(film[a].name.compare(film[a+1].name) == 1)
{
swap(film[a], film[a+1]);
swaps = 1;
}
}
}
}
int main()
{
string st;
int len, i = 0, x = 0;
ifstream films ("x.x");
if(films.is_open())
{
while(!films.eof())
{
for(x = 0; x < 10; x++)
{
getline(films, st);
film[x].name = st;
getline(films, st);
film[x].date = st;
film[x].count = 0;
getline(films, st);
do
{
len = st.length();
film[x].star[i] = st;
film[x].starx[i] = film[x].star[i].substr(' ');
film[x].count++;
getline(films, st);
i++;
}while(st[0] != '~' && !films.eof());
sort_stars(film[x].star, film[x].starx, i);
i = 0;
}
}
sort_movie(film, 10);
for(int a = 0; a < 10; a++)
{
cout << film[a].name << endl;
cout << film[a].date << endl;
for(int b = 0; b < film[a].count; b++)
{
cout << film[a].star[b] << endl;
}
cout << endl;
}
films.close();
}
return 0;
}
That's what I've got. I tried it with other data (just edited it a little bit) and it did the job, but now I'm getting this error:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr
Aborted
after I run the program with the data I'm using.
Quote
Journey 2: The Mysterious Island
February 10, 2012
Josh Hutcherson
Dwayne Johnson
Michael Caine
~
Project X
March 2, 2012
Thomas Mann
Oliver Cooper
Jonathan Daniel Brown
~
Wrath of the Titans
March 30, 2012
Sam Worthington
Liam Neeson
Rosamund Pike
~
The Lucky One
April 20, 2012
Zac Efron
Taylor Schilling
Blythe Danner
Riley Thomas Stewart
Jay R. Ferguson
Adam LeFevre
Robert Hayes
Joe Chrest
~
Dark Shadows
May 11, 2012
Johnny Depp
Michelle Pfeiffer
Eva Green
~
Chernobyl Diaries
May 25, 2012
Jesse McCartney
Jonathan Sadowski
Olivia Dudley
~
Rock of Ages
June 15, 2012
Julianne Hough
Diego Boneta
Tom Cruise
~
Magic Mike
June 29, 2012
Channing Tatum
Alex Pettyfer
Olivia Munn
~
The Dark Knight Rises
July 20, 2012
Christian Bale
Tom Hardy
Anne Hathaway
~
The Campaign
August 10, 2012
Will Ferrell
Zach Galifianakis
Jason Sudeikis
Dylan McDermott
Katherine LaNasa
Sarah Baker
John Lithgow
Dan Aykroyd
Brian Cox
February 10, 2012
Josh Hutcherson
Dwayne Johnson
Michael Caine
~
Project X
March 2, 2012
Thomas Mann
Oliver Cooper
Jonathan Daniel Brown
~
Wrath of the Titans
March 30, 2012
Sam Worthington
Liam Neeson
Rosamund Pike
~
The Lucky One
April 20, 2012
Zac Efron
Taylor Schilling
Blythe Danner
Riley Thomas Stewart
Jay R. Ferguson
Adam LeFevre
Robert Hayes
Joe Chrest
~
Dark Shadows
May 11, 2012
Johnny Depp
Michelle Pfeiffer
Eva Green
~
Chernobyl Diaries
May 25, 2012
Jesse McCartney
Jonathan Sadowski
Olivia Dudley
~
Rock of Ages
June 15, 2012
Julianne Hough
Diego Boneta
Tom Cruise
~
Magic Mike
June 29, 2012
Channing Tatum
Alex Pettyfer
Olivia Munn
~
The Dark Knight Rises
July 20, 2012
Christian Bale
Tom Hardy
Anne Hathaway
~
The Campaign
August 10, 2012
Will Ferrell
Zach Galifianakis
Jason Sudeikis
Dylan McDermott
Katherine LaNasa
Sarah Baker
John Lithgow
Dan Aykroyd
Brian Cox
List of stars are also supposed to be sorted alphabetically by last name.

New Topic/Question
Reply


MultiQuote




|