-2
#include <iostream>
#include <string.h>
#include <string>
#include<fstream>
struct time
{
    int hours, minutes;
};

struct moviedata
{
    string moviename, genre, actorname1, actorname2;
    int yearreleased, priceperday;
    time duration;

};

void readmoviedata(moviedata *ptr) /*not ale to run this code*/
{
    int i = 0;
    string x;
    ifstream inside("movies.txt");
    while (!inside.eof())
    {

        getline(inside, ptr[i].moviename);
        inside >> ptr[i].yearreleased;
        getline(inside, ptr[i].genre);
        inside >> ptr[i].duration.hours;
        inside >> ptr[i].duration.minutes;
        getline(inside, ptr[i].actorname1);
        getline(inside, ptr[i].actorname2);
        inside >> ptr[i].yearreleased;
        i++;
    }
}
int main()
{
    moviedata *md = new moviedata;

    readmoviedata(md);
    delete md;
    return 0;
}

can anyone tell me what i did wrong with function readmoviedata $ when runned line by line as soon as the function readmoviedata is called $ black grid appears with cursor blinking and nothin else

user4581301
  • 29,019
  • 5
  • 26
  • 45
  • `moviedata *md = new moviedata;` creates one single object. `md[i]` is only valid for `i == 0`. Use `std::vector` instead. – HolyBlackCat Apr 16 '18 at 20:06
  • `while (!inside.eof()) ` https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – drescherjm Apr 16 '18 at 20:33

1 Answers1

0

Problem is that you are creating the one pointer variable of struct type.

moviedata *md = new moviedata;

and you are treating this variable like array.

 ptr[i].yearreleased;
Muhammad Usman
  • 1,090
  • 11
  • 22
  • I changed moviedata *md = new moviedata[15] its still not working – blackpanther Apr 16 '18 at 20:29
  • 1
    @blackpanther you have three or more bugs. Muhammad has shown you one and you have fixed it. drescherjm left you a comment about another. A Third is outlined at [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – user4581301 Apr 16 '18 at 20:55