Hi, how do you read multiple data types in a file and storing it in a linked list. I read the items with no linked list but when I used linked list it doesnt work. I think there is something wrong in my code. Programming language: C++ Thank you

struct Video {
    int videoID;
    string movieTitle;
    string movieGenre;
    string movieProduction;
    int numberOfCopies;

};



class videoList {
    private:
        struct videoNode {
            Video video;
            struct videoNode *next;
        };
        videoNode *head;

    public:
        videoList(); //constructor
        //~videoList(); //deconstructor
        void insertNewVideo (Video vid);
        void rentVideo (Video vid);
        void returnVideo (Video vid);
        void showVideoDetails (Video vid);
        void displayVideo ();
        bool isVideoExist();
        void exit ();
        void generateVideoID();
        void readVideoFile();
};



void videoList::readVideoFile () {
    videoNode *nodePtr;
    ifstream file( "videoList.txt" ) ;
    string line ;
    string id, movieTitle, movieGenre, movieProduction, numberOfCopies;

    if (!file) {
        cout << endl << "\t\t\t\t There is file!" << endl;
    }
    else {
        std::stringstream linestream(line);
        cout << endl << "\t\t\t\t Movie List " << endl; 
        nodePtr = head;

        while (nodePtr)
            while (getline(file, line)){
    {


        std::getline(linestream, id, ',');
        std::getline(linestream, nodePtr->video.movieTitle, ',');
        std::getline(linestream, nodePtr->video.movieGenre, ',');
        std::getline(linestream, nodePtr->video.movieProduction, ',');
        linestream >> nodePtr->video.numberOfCopies;

        if(linestream) // true if there were no errors reading the stream
        {
            std::cout << id << '\n';
            std::cout << nodePtr->video.movieTitle << '\n';
            std::cout << nodePtr->video.movieGenre << '\n';
            std::cout << nodePtr->video.movieProduction << '\n';
            std::cout << nodePtr->video.movieTitle << '\n';
        }
    }
}
}
}

Hi, I don't know what exactly do you mean by "it doesnt work", but I see at least 2 possible problems with your code:

  1. unfortunatelly you did not show us the code for constructor function videoList::videoList(), so we don't know to what value the variable head is initialized, but if it is nullptr, then after "nodePtr = head;" the body of the loop "while (nodePtr){...}" is skipped and the function simply terminates without reading a single line from the file
  2. you do not actually build a linked list, because you are not creating any new "videoNode" instances and you are not setting "next" pointer to point to next structure istance in the list
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.