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