Ignored Function after .txt scan loop

-1

My school has given me a homework to accomplish, but I've got a problem that I really can't understand its reason.

The main goal of the assignment is that we scan a txt file that the teacher gave us and do some operations with it, and of course we will use the objects here. However, the IDE does not detect my function in a line after exiting the "while" loop where the txt file is scanned and data is retrieved from there. I was using CLion, but even though I tried it in DEV C ++, I encountered the same problem.

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>


#define MAX 50
#define EXIT exit(1)

#define Beginning_ID 1500
using namespace std;

 class Movie{

  private:
      int id;
      string name;
      float point;
      int year;
  public:

      int getId(){ return id;}
      string getName(){ return name; }
      float getPoint(){ return point; }
      void setPoint(float point){ this->point = point; }
      int getYear(){ return year; }
      Movie(int id, string name, float point, int year){
          this->id = id;
          this->name = name;
          this->point = point;
          this->year = year;
      }
      Movie(){
          /* Dummy constructor */
      }
      void printMovie(){
          cout << "MOVIE: " << name << " -- " << "YEAR: (" << year;
          cout << ") -- " << "AVG. RATING: " << point << " -- ID: " << id << "\n";
      }

 };

Basically, I have to create a Movie class, and in the main function; I have to create a Movie array to store Movies from the .txt file that I've been trying to scan. In the .txt file, I have to get Movie's "name" , "average point", "and its year" and store them on a created Movie object. Apart from this, some other functions were requested to be wrote in the code.

int autoIncrement(int incrementCount){
    /* Function to initialize ID according to starting ID value "Beginning_ID" */
    return Beginning_ID + incrementCount;
}


void movie_Details_wID(Movie *movieArray[], int size, int ID) {  
      /* Function allows user to get the Movie by its ID */

    int i = 0;
    for(; i < size ; i++){
        if(movieArray[i]->getId() == ID)
            movieArray[i]->printMovie();
    }

}


int main(){

    ifstream fileReader;

    fileReader.open(" /* text file location string */ ");
    if(!fileReader.is_open()){
        cerr << "File cannot be opened." << endl;
        EXIT;
    }

    string line;
    string name;
    string point;
    string year;

    Movie* top50 = new Movie[MAX];
    int count = 0;

    while(getline(fileReader, line)){

        stringstream lineStream(line);
        getline(lineStream, point, ';');
        getline(lineStream, name, '(');
        getline(lineStream, year, ')');


        top50[count] = Movie(autoIncrement(count), name, stof(point), stoi(year));

        top50[count].printMovie();

        count++;

    }

    movie_Details_wID(&top50, MAX, 1545);  // ! THIS IS THE IGNORED LINE //
    fileReader.close();


    return 0;
}

My other functions are successfully working, and I can retrieve data from the given .txt file. After the while loop, I wanted to test my movie_Details_wID function. There was a movie with the ID "1545" in the top50 Movie array. But function does not work or my line has been ignored by IDE. I've worked with CLion and its output is:

 CLion:

   MOVIE: The Shawshank Redemption  -- YEAR: (1994) -- AVG. RATING: 9.2 -- ID: 1500
   MOVIE: The Godfather  -- YEAR: (1972) -- AVG. RATING: 9.1 -- ID: 1501
   MOVIE: The Godfather: Part II  -- YEAR: (1974) -- AVG. RATING: 9 -- ID: 1502
   MOVIE: Il buono, il brutto, il cattivo.  -- YEAR: (1966) -- AVG. RATING: 8.9 -- ID: 1503
                .
                .
                .
   
   Process finished with exit code -1073741819 (0xC0000005)
c++
clion
asked on Stack Overflow Nov 7, 2020 by Ryuulan

1 Answer

0

Your basic problem is one of incorrect types and incorrect logic in accessing data of those types. This:

void movie_Details_wID(Movie *movieArray[], int size, int ID)

expects a sequence of pointers to Movie objects. But you're passing it this:

Movie* top50 = new Movie[MAX];
...
movie_Details_wID(&top50, MAX, 1545);

That's not a sequence of pointers; that's the address of a single pointer. It points to a sequence of Movie objects. In short you have a single pointer to a sequence of objects; not a sequence of pointers to objects as your function expects.

Change your function to be:

void movie_Details_wID(Movie movieArray[], int size, int ID) 
{
    /* Function allows user to get the Movie by its ID */
    int i = 0;
    for(; i < size ; i++){
        if(movieArray[i].getId() == ID)
            movieArray[i].printMovie();
    }
}

and change the code in main to do this:

movie_Details_wID(top50, count, 1545); 

Note that I also fixed the size argument; you should pass the number of actual records; not the max count.

Frankly, that will fix this code,or at least get it close to what you need, but in reality you should be using a std::vector<Movie> for this, and either using iterators a passing it by const-reference in entirety to your function.

answered on Stack Overflow Nov 7, 2020 by WhozCraig

User contributions licensed under CC BY-SA 3.0