Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I compile my code I am getting this killed message. Why is this happening and how do I fix it?
EDIT: According to Richard I may need to fix my addRec function. I'll be updating my code when (hopefully) I figure the issue.

Also this program is a playlist program that takes song information from user (and input file and orders it from greatest to least. It also allows the user to delete, print songs, show songs of one artist, and removes songs with less than a specific amount of likes.

alizah.khan@syccuxas01:~/cs260/project/project1> make
g++    -c -o main.o main.cpp
g++    -c -o songs.o songs.cpp
g++ -ggdb main.o songs.o -o output
alizah.khan@syccuxas01:~/cs260/project/project1> ls
main.cpp  main.o  makefile  output  songs.cpp  songs.h  songs.o  songs.txt
alizah.khan@syccuxas01:~/cs260/project/project1> ./output
Killed
alizah.khan@syccuxas01:~/cs260/project/project1>


MYMAKEFILE
XXFLAGS = -Wall -Wextra -ggdb

OBJS = main.o songs.o

output: $(OBJS)
        $(CXX) -ggdb $^ -o $@

songs.o: songs.cpp songs.h

clean:
        rm -f $(OBJS) output

//songs.h
#pragma once
#include <iostream>
#include <fstream>
#include <cstring>


struct node {
  char * name;
  char * title;
  double length;
  int likes;
  node * next;
};

class SongList {
  public:
    SongList();
  ~SongList();

  bool add(char * & , char * & , double & , int & );
  bool addRec(node * curr, node * data);
  bool editLikes(char * & , char * & , int & );
  const bool showAll();
  const bool showArtist(node * curr,
  const char * );
  bool removeByLikes(int & );
  bool deleteRec(node * );
  const bool printRec(node * );

  private:

  node * head;
  node * temp;
  node * tail;
};


//Alizah Khan
//songs.cpp
#include "songs.h"

SongList::SongList(): head(nullptr), tail(nullptr) {}

SongList::~SongList() {

}

//adds a song to the list in the correct place according to the likes
bool SongList::add(char * & dataName, char * & dataTitle, double & dataLength, int & dataLikes) {

  node * newNode = new node;

  //set data
  newNode -> name = dataName;
  newNode -> title = dataTitle;
  newNode -> length = dataLength;
  newNode -> likes = dataLikes;

  //check if list is empty yes; set the tail and head to new node
  if (head == nullptr) {
    head = newNode;
    tail = newNode;
    return true;
  }

  //recursion if  not the head
  return addRec(head, newNode);
}

//Helper add function
bool SongList::addRec(node * curr, node * data) {

  //if the newNode has a more likes than the head = new head
  if (data -> likes >= head -> likes) {
    data -> next = head;
    head = data;
    return true;
  }

  //if the next song has less likes insert data before it
  else if (data -> likes >= curr -> likes) {
    data -> next = curr;
    temp -> next = data;
    return true;
  }

  //if end list is reached with no inserting. Make the data the new tail
  else if (curr == tail) {
    tail -> next = curr;
    curr = tail;
    return true;
  }

  //traverse list if next has greater likes
  else {
    temp = curr;
    return addRec(curr -> next, data);
  }
}

//prints the nodes recursively
const bool SongList::printRec(node * curr) {

  //What we are going to do
  std::cout << "Name of artist is:" << curr -> name << std::endl;
  std::cout << "Name of song is:" << curr -> title << std::endl;
  std::cout << "How long song is:" << curr -> length << std::endl;
  std::cout << "How many likes are there:" << curr -> likes << std::endl;

  //basecase
  if (curr == tail)
    return true;

  //recursion
  return printRec(curr -> next);
}
//helps print func
const bool SongList::showAll() {

  if (head == nullptr) {
    std::cout << "List is empty/n";
    return false;
  }

  return printRec(head);

}

//Prints all song data if they have spesific artist
const bool SongList::showArtist(node * curr,
  const char * artist) {

  if (curr -> name == artist) {
    std::cout << "Name of artist is:" << curr -> name << std::endl;
    std::cout << "Name of song is:" << curr -> title << std::endl;
    std::cout << "How long song is:" << curr -> length << std::endl;
    std::cout << "How many like are there: " << curr -> likes << std::endl;
  }
  if (curr == tail) {
    return true;
  }
  return showArtist(curr -> next, artist);
}

//recursively delete songs from given node
bool SongList::deleteRec(node * curr) {

  if (head == nullptr)
    return false;

  temp = curr;
  delete curr;
  return deleteRec(temp -> next);
}

//Remove all songs from the list if it has less than M likes
bool SongList::removeByLikes(int & dataLikes) {

  node * travPtr = head;

  while (travPtr -> next -> likes > dataLikes) {
    travPtr = travPtr -> next;
  }
  travPtr = tail;
  return deleteRec(travPtr -> next);
}


//main.cpp
//Alizah Khan
//takes file and makes linked list
#include "songs.h"

using namespace std;

int main() {

  cout << "1";

  //variables
  ifstream inFile;
  SongList list;
  char medium[128];
  char * artist, * sName;
  double size;
  int likes;

  //open the song file
  inFile.open("songs.txt");

  if (inFile.fail()) {
    cout << "File failed to open, please fix and run again!" << endl;
    return -1;
  }

  //read the file into our linked list
  while (!inFile.eof()) {

    inFile.getline(medium, 128, ',');
    artist = new char[sizeof medium];
    inFile.getline(medium, 128, ',');
    sName = new char[sizeof medium];
    inFile >> size;
    inFile.ignore(',');
    inFile >> likes;
    inFile.ignore('\n');

    list.add(artist, sName, size, likes);
  }


//greet user and start menu
cout << "Welcome to SONGLIST! \nPlease enter your option from the menu. \n" <<
  "Press (A) to add a song." <<
  "Press (E) to edit the likes of a song.\n" <<
  "Press (D) if you want to show all songs.\n" <<
  "Press (T) if you want to show songs by artist.\n" <<
  "Press (R) to remove all songs with less than m likes.\n";

return 0;
}


THIS IS THE INPUT FILE THAT I AM USING FROM SOMONE ELSE.
BTS, DYNIMITE, 3.43, 41000000
NF, CLOUDS, 1.14, 869000
HARRY, FALLING, 3.27, 30000000
RACHEL, FIGHT SONG, 3.23, 166000
RUTH, LOST BOY, 4.33, 65000
JAMES, SAY YOU WONT LET GO, 3.27 213000
HARRY, SIGN OF THE TIMES, 5.41, 63000000
IDENA, LET IT GO, 3.21, 21000000
BTS, BLACK SWAN, 3.43, 23000000
PASSENGER, LET HER GO, 4.20, 97000
KATIE, DAISIES, 2.53, 12000
BEBE, SACRIFICES, 2.40, 300000
ANDY, DONT GIVE UP ON ME, 3.16, 5600000
JUSTIN, BIGGER THAN, 3.28, 21000
SAM, IM READY, 3.20, 30500
ALEC, OH MY GOD, 3.07, 1000000
ANDY, GOOD EXAMPLE, 2.29, 15000
GREYSON, GOOD AS GOLD, 3.25, 78000
BRYNN, TELL ME IM PRETTY, 3.08, 29000
GRYFFIN, CRY, 3.38, 3000000
KHALID, FREE SPIRIT, 3.02, 55000
KYGO, LIKE IT IS, 3.03, 890000
CHAINSMOKERS, THIS FEELING, 3.17, 760000
KASKADE, WITH YOU, 3.00, 2000
LAUV, LIKE ME BETTER, 3.17, 5000000
AVA, SALT, 3.00, 56900
LEWIS, BEFORE YOU GO, 3.50, 89000
LAUV, NEVER NOT, 4.02, 76000
JEREMY, ALWAYS ILL CARE, 2.40, 68000
JAMES, IMPOSSIBLE, 4.07, 49000
GMILLER, NO TEARS LEFT TO CRY, 3.10, 770000


What I have tried:

I don't know what to do so I haven't tried anything really.
EDIT: I ran my code through this online website
https://www.onlinegdb.com/online_c++_debugger
The website said this. But I don't know which one so I tried shortening the input file and looking over my code but I still don't get it.
"If your program is reading input from standard input and you forgot to provide input via stdin.
Your program contains infinite loop, which may never break.
Your program contains infinite recursive function calls.
May be your program is trying to process large data and it takes much time to process"
Posted
Updated 28-Apr-21 10:14am
v4
Comments
[no name] 28-Apr-21 0:22am    
If anything needs to be fixed lemme know XD
Rick York 28-Apr-21 0:49am    
The call to list.add outside the while loop does not look correct.
[no name] 28-Apr-21 0:55am    
That part isn't in my code I mistakenly added it in the question tho thanks for letting me know.
Richard MacCutchan 28-Apr-21 3:48am    
It could be that your recursive addRec method is consuming all of memory.
[no name] 28-Apr-21 3:57am    
What would be a better recursive addRec method? Cuz my brain's gone completely blank I've tried a few different things.

1 solution

I think you should revise your design. Personally, I would use an STL container to contain the nodes but if you don't want to or can't that's OK. Anyway, nodes should be a song class. That class should contain all information about the song along and should have methods to set the members, to read itself, to write itself, and to display itself. The SongList class should contain and manage a list of songs. As an example, the SongList class should read the file line by line and call a method of the song class to translate that line and set its members to the appropriate values. When the members of the song object are set then the SongList class should add that instance to its list.

By partitioning this way you can use different classes to contain the instances of the song objects. You have a linked list going now. I would use a std::list to do this as it handles insertion and deletion better than std::vector does but if you want or need to do this yourself, think about writing the container of the objects in a generic way. The term generic here means the container should work the same regardless of the objects it contains and it is best if it doesn't know any of the particulars of the object. This is where templates are very useful. The important thing is the object shouldn't have to know the particulars about how it is stored - it should work the same regardless.
 
Share this answer
 
Comments
[no name] 28-Apr-21 16:53pm    
It seems that there was a problem with reading from the file actually so I fixed it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900