-1

I have an existing program that takes in input with 5 parameters. The inputs are a video title, url, comment, length, and rating. All contained in video.cpp and video.h. Those inputs are then inserted into the linked list, vlist.cpp and vlist.h, in sorted order, according to the video title. Once the sorting is complete, I need to be able to print the new sorted list.

Note: I am not allowed to use any form of input or output in my Vlist class. However, I can call Video's print function.

My problem occurs when I try to call video.cpp's print function in void Vlist::print() (or so I think). I keep getting a segmentation error.

Here is my code:

main.cpp

#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;

#include "video.h"
#include "vlist.h"


int main()
{
  string sort_type, url, comment, title;
    int rating;
    double length;
    int initial = 0, last = 0;

    Video *videoObj; 
    Vlist *vlistObj;

  while (getline(cin,title)) {

        getline(cin, url);
        getline(cin, comment);
        cin >> length;
        cin >> rating;
        cin.ignore();


        vlistObj->Insert(videoObj);
        initial++;
        last++;
    }

        vlistObj -> print();

return 0;
}

video.cpp

#include <iostream>
#include <algorithm>
using namespace std;

#include "video.h"
#include "vlist.h"

Video::Video(string video_title, string video_link, string video_comment, double video_length, int video_number)
  : title(video_title), link(video_link), comment(video_comment), length(video_length), rating(video_number)
{
  name = title;
}


bool Video::Title(Video *spare3)
{
  return name > spare3-> name;

}
void Video::print(){

  string hierarchy;
  switch(rating){

    case 1:
      hierarchy = "*";
      break;
    case 2:
      hierarchy = "**";
      break;
    case 3:
      hierarchy = "***";
      break;
    case 4:
      hierarchy = "****";
      break;
    case 5:
      hierarchy = "*****";
      break;

  }

  cout << title << ", " << link << ", " << comment << ", " << length << ", " << hierarchy << endl;

}

video.h

#ifndef VIDEO_H
#define VIDEO_H

#include "vlist.h"

class Vlist;

class Video {

  public:
    Video(string video_title, string video_link, string video_comment, double video_length, int video_number);
    void print();
    bool Rating(Video *spare);
    bool Length(Video *spare2);
    bool Title(Video *spare3);
    const string& GetTitle() const { return title; }




  private:

    std::string title;
    string name;

    string link;
    string comment;
    double length;
    int rating;

};


#endif

vlist.h

#ifndef VLIST_H
#define VLIST_H

#include "video.h"



// forward declaration of class Video
 class Video;

class Vlist {
 public:

 Vlist() {m_head = nullptr; }
 void Insert(Video *video);
  void print();
 private:
 class Node {
        public:
                Node(Video *video, Node *next) {m_video = video; m_next = next; }
                Video *m_video;
                Node *m_next;
            };
            Node *m_head;
 };




#endif

vlist.cpp

#include <iostream>
#include <algorithm>
using namespace std;

#include "vlist.h"
#include "video.h"




void Vlist::Insert(Video* video)
{
    if (m_head == NULL || m_head->m_video -> GetTitle() > video->GetTitle())
    {
        // Insert before head...
        m_head = new Node(video, m_head);

    }
    else
    {
        // Insert after existing node...
        Node *node = m_head;
        while (node->m_next != NULL && node->m_next -> m_video->GetTitle() < video->GetTitle())
        {
            
            node = node->m_next;
        }
        node->m_next = new Node(video, node->m_next);

    }

 }

 void Vlist::print()
 {
     Video *video;
     Node *node = m_head;

     while(node != NULL)
     {
         video->print();
         node = node->m_next;
     }

 }

Also note that I am aware that some of my code in main.cpp and vlist.cpp are incomplete, I am merely trying to work in parts. Which is currently receiving an output back.

Again, I am not completely sure why I am getting a segmentation fault. The program only gives me a seg fault whenever I am done with my input. I am still very new to linked lists and even pointers, thus, any help would be appreciated

Fooji
  • 71
  • 7
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Oct 02 '20 at 01:53
  • @AndreasWenzel I did use `gbd a.out`, but honestly I am still very confused on the problem at hand. As it mentioned that the problem is in `video.cpp`, however I have tested `video.cpp` and received the correct output back (before inputting into the Linked list). The error only occurred when I input `void Vlist::print()`. But I will take a further look into the links you have provided – Fooji Oct 02 '20 at 02:00
  • If you are already aware of gdb, then I don't think that you need to read the links that I posted. It may still be an interesting read, though. – Andreas Wenzel Oct 02 '20 at 02:04
  • 1
    You could simplify your example code by dropping most of your assigned requirements. Don't read values from the user; hardcode the values to use (or focus on just getting values from the user, and drop everything after that). A video's title might be important since it is the sort key, but drop all other data from `Video`. Keep removing unneeded stuff until the error has no place to hide. **In addition:** [always enable compiler warnings](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) and address those warnings before posting your [mre]. – JaMiT Oct 02 '20 at 02:27

1 Answers1

2

in main function, you must allocate pointer values.

  main(){
 Video *videoObj; 
    Vlist *vlistObj;
} // error

main(){

    Vlist *vlistObj = new vlistObj;
    while(...){
       ...
       Video *videoObj = new videoObj(title,url,...);
       vlistObj->Insert(videoObj);
       ...
    }
}