3 Replies - 967 Views - Last Post: 18 September 2012 - 09:41 AM Rate Topic: -----

#1 Travis L   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-April 12

"Terminate called"

Posted 18 September 2012 - 07:29 AM

So, I've got this problem with my program right now. Objective is to input a file with a list of 10 movies with a small list of stars, then sort both the movies and stars in alphabetical order, then print the list.

#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


List of stars are also supposed to be sorted alphabetically by last name.

Is This A Good Question/Topic? 0
  • +

Replies To: "Terminate called"

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: "Terminate called"

Posted 18 September 2012 - 07:33 AM

Look at the following snippet:
film[x].starx[i] = film[x].star[i].substr(' ');

Then read the documentation for std::string.substr() and note the type of parameters, and what happens when you try to use a value for the first parameter that is longer than your string. On a very related note, what is the decimal value of the space character?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Travis L   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-April 12

Re: "Terminate called"

Posted 18 September 2012 - 07:53 AM

Thanks - I should've looked at that first..
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

Re: "Terminate called"

Posted 18 September 2012 - 09:41 AM

> 58 while(!films.eof())
This is not the correct way to read a file.
http://sourceforge.n....php?title=Feof

eof() only becomes true AFTER a previous file reading function such as getline() has FAILED.
But by then, in your code, you've been processing erroneous data.

If you assume a well-formed file, then you can get away with testing the first getline only.

Eg.
const int maxFilms = 10;
while ( x < maxFilms && getline(films, st) ) {
}


Declare the const towards the beginning of your code, and use that in place of all those magic 10's you have all over the place.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1