Question: I'm having issues with compiling my program. What should I do? The makefile used in PuTTY: output: main.o songs.o g++ main.o songs.o -o output main.o:
I'm having issues with compiling my program. What should I do?
The makefile used in PuTTY:
output: main.o songs.o g++ main.o songs.o -o output main.o: main.cpp g++ -c main.cpp songs.o: songs.cpp songs.h g++ songs.cpp clean: rm main rm *.o output
The input file:
BTS, DYNAMITE, 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
//main.cpp
//This is a program that makes a linked list of the data in an external file
#include "songs.h"
#include
#include
#include
#include
int main() {
using namespace std;
cout << "1";
//variables
ifstream inFile;
SongList list;
char Med[128];
char *artist,*songName;
double length;
int likes;
char medium[128];
//open the song file
inFile.open("songs.txt");
if (inFile.fail()){
cout << "File failed to open... Check for error and try again.!" << endl;
return -1;
}
//read the file into the linked list
while (!inFile.eof()){
inFile.getline(Med, 128, ',');
artist = new char[sizeof Med];
inFile.getline(medium, 128, ',');
songName = new char[sizeof Med];
inFile >> length;
inFile.ignore(',');
inFile >> likes;
inFile.ignore(' ');
list.add(artist, songName, length, likes);
}
//Welcome user
cout << "Welcome to SONGLIST! Please enter your option from the menu. " <<
"Press (A) to add a song." <<
"Press (E) to edit the likes of a song. " <<
"Press (D) if you want to show all songs. " <<
"Press (T) if you want to show songs by artist. " <<
"Press (R) to remove all songs with less than m likes. ";
return 0;
}
//songs.h
//songs.h
#pragma once
#include
#include
#include
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;
};
songs.cpp
#include "songs.h"
#include
#include
#include
SongList::SongList(): head(nullptr), tail(nullptr){}
SongList::~SongList(){
}
//adds a song to the list in the correct part according to likes
bool SongList::add(char *&dataName, char *&dataTitle, double& dataLength, int& dataLikes){
node* newNode= new node;
//set the data
newNode->name = dataName;
newNode->title = dataTitle;
newNode-> length = dataLength;
newNode->likes = dataLikes;
//check if list is empty, if so this will set the tail+head > new node
if (head == nullptr){
head = newNode;
tail = newNode;
return true;
}
//Recursion if not head
return addRec(head, newNode);
}
//Add func help
bool SongList::addRec(node* curr, node* data){
//if the newnode has more likes than head becomes the new head
if (data->likes >= head->likes){
data->next = head;
head = data;
return true;
}
//if the next song has likes > the data song then -> the data before it
else if (data->likes >= curr->likes){
data->next = curr;
temp->next = data;
return true;
}
//No inserting end of list then make the data the new tail
else if(curr == tail){
tail->next = curr;
curr = tail;
return true;
}
//traverse the list if greater likes
else {
temp = curr;
return addRec(curr->next, data);
}
}
//func to print nodes recursively
const bool SongList::printRec( node *curr){
// the thing we wanna do
std::cout << "The name of the artist is" << curr->name << std::endl;
std::cout << "The title of the song is" << curr->title << std::endl;
std::cout << "The length of the song is" << curr->length << std::endl;
std::cout << "The number of likes on the song are" << curr->likes << std::endl;
//base case
if (curr == tail )
return true;
//recursion
return printRec(curr->next);
}
//Help print func
const bool SongList::showAll(){
if (head == nullptr){
std::cout<< "List is empty/n";
return false;
}
return printRec(head);
}
//Prints out all songs with data if they have a spesific artist
const bool SongList::showArtist(node* curr, const char *artist) {
if (curr->name == artist){
std::cout << "The name of the artist is" << curr->name << std::endl;
std::cout << "The title of the is" << curr->title << std::endl;
std::cout << "The length of the song is" << curr->length << std::endl;
std::cout << "The number of likes on the song are" << curr->likes << std::endl;
}
if (curr == tail){
return true;
}
return showArtist(curr->next, artist);
}
//func to delete all songs from a node
bool SongList::deleteRec(node* curr){
if (head == nullptr)
return false;
temp = curr;
delete curr;
return deleteRec(temp->next);
}
//func to remove all songs from the list if it has less anything 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);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
