Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
//This is my code. I don't understand why it is not compiling. What are the errors and how would the makefile for this program look?
My input file looks like this:
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

C++


main.cpp

//Song Linked list Program #1

//Program that takes a file and makes a linked list of all of the data

#include "songs.h"

using namespace std;



int main() {



cout << "1";



//variables

ifstream inFile;

SongList list;

char medium[128];

char *artist,*songName;

double length;

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, ',');

songName = new char[sizeof medium];

inFile >> length;

inFile.ignore(',');

inFile >> likes;

inFile.ignore('\n');



list.add(artist, songName, length, likes);

}





//allocate memory for char *

/*

tl = new char[t.length() + 1];

ar = new char[a.length() + 1];

*/





//greet user and start menu

cout << "Welcome to the songlist program! \nPlease enter the function that you would like to use! \n" <<

"Press (A or a) to add a song!\n" <<

"Press (E or e) to edit the likes of a song!\n" <<

"Press (D or d) to display all songs!\n" <<

"Press (T or t) to display songs by artist!\n" <<

"Press (R or r) to remove all songs with less than x many likes!\n";



return 0;

}

___________________________________________________________________________________________________________________________________________________________

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 addRecursively(node* curr, node* data);

bool editLikes(char*&, char*&, int&);

const bool displayAll();

const bool displayArtist(node* curr, const char*);

bool removeByLikes(int&);

bool deleteRecursively(node*);

const bool printRecursively(node*);





private:



node* head;

node* temp;

node* tail;

};

__________________________________________________________________________________________________________________________________

songs.cpp

#include "songs.h"



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









SongList::~SongList(){



}





//adds a song to the list in the correct spot to do with 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 it is set the tail and head to the new node

if (head == nullptr){

  head = newNode;

  tail = newNode;

  return true;

}



//do some recursion if it's not the head

return addRecursively(head, newNode);

}





//Helper add function

bool SongList::addRecursively(node* curr, node* data){



//if the newNode has a bigger like like than the head it becomes the new head

if (data->likes >= head->likes){

  data->next = head;

  head = data;

  return true;

}



//if the next song has less likes than our data song then insert our data before it

else if (data->likes >= curr->likes){

  data->next = curr;

  temp->next = data;

  return true;

}



//if we reach the end of the list without inserting then make the data the new tail

else if(curr == tail){

  tail->next = curr;

  curr = tail;

  return true;

}



//traverse the list if the next piece of data has greater likes

else {

temp = curr;

return addRecursively(curr->next, data);

}

}







//function to print out all of the nodes recursively

const bool SongList::printRecursively( node *curr){



// the thing we wanna do

std::cout << "Song Name is" << curr->name << std::endl; 

std::cout << "Song title is" << curr->title << std::endl; 

std::cout << "Song length is" << curr->length << std::endl; 

std::cout << "Song Likes # is" << curr->likes << std::endl;  



//base case

if (curr == tail )

return true;



//recursion

return printRecursively(curr->next);

}

//Helper print function

const bool SongList::displayAll(){



if (head == nullptr){

std::cout<< "List is empty/n";

return false;

}



return printRecursively(head);



}







//function to print out all songs with data if they have a certain artist

const bool SongList::displayArtist(node* curr, const char *artist) {



if (curr->name == artist){

std::cout << "Song Name is" << curr->name << std::endl; 

std::cout << "Song title is" << curr->title << std::endl; 

std::cout << "Song length is" << curr->length << std::endl; 

std::cout << "Song Likes # is" << curr->likes << std::endl;  

}

if (curr == tail){

  return true;

}

return displayArtist(curr->next, artist);

}



//function to recursively delete all songs from the given node

bool SongList::deleteRecursively(node* curr){



if (head == nullptr)

  return false;



temp = curr;

delete curr;

return deleteRecursively(temp->next); 

}







//function to remove all songs from the list if it has less than so many likes

bool SongList::removeByLikes(int &dataLikes){



node* travPtr = head;



while (travPtr->next->likes > dataLikes){

  travPtr = travPtr->next;

}

travPtr = tail;

return deleteRecursively(travPtr->next);

}


What I have tried:

I have tried debugging and checking my code over and over again but I haven't found out how to fix the error or errors.
Posted
Updated 26-Apr-21 21:49pm
v4
Comments
Stefan_Lang 27-Apr-21 3:25am    
I see v3 of your question, and it looks like you accidentally replaced the text of your question with the input file. Please correct.
Richard MacCutchan 27-Apr-21 4:13am    
I think he did that deliberately in response to OriginalGriff's comments.

Quote:
I have tried debugging and checking my code over and over again but I haven't found out how to fix the error or errors.

No, you haven't.
You can't debug code that doesn't compile: debugging is the process of removing logic errors from your code and that only starts to happen when your code is running - because until then it doesn't have any logic errors at all.

Errors which mean that your code doesn't compile are called "syntax errors" and they are generally caused by your typing the wrong thing - spelling a name wrong, wissing out or adding a comma, semicolon, or bracket for example.

Now, I'm not going to sit here and wade through nearly 480 lines of double spaced, unindented, badly commented code to try and spot where your syntax errors are: particularly when I don't have access to the include file "songs.h" so I can't even begin to try compiling it to find out what your syntax errors might be.

So do three things:
1) Stop double spacing your code: it doesn't make it look bigger, or better - it makes it harder to read because there is less of it on the screen so it's harder to see whole functions at once and get a sense of how they work.
2) Indent your code. Pick an indentation style and stuck to it - don't "mix and match" as you do in that code:
C++
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())

{
It makes it much, much harder to see what is going on - particularly if there may be a missing close bracket for example.
Getting your indentation right makes your code more readable and makes it a load more obvious when you do miss something out.
3) When you have done all that, compile it. Look at the first error, and pay attention to what it says: it will give you a file name, a line and column number, and a message.
The file name is obvious, the line and column numbers tell you exactly where it found a problem, and the message describes what the problem was, and they are invaluable in working out a solution. Without them, you pretty much can't fix squat!

Remember though that the error line is where the compiler noticed the problem, not necessarily where the problem was caused: if you miss out a close bracket for example, it may not get "noticed" until the end of the file. The error ,message helps a lot in that regard!
 
Share this answer
 
Comments
Jerry Jeremiah 4-May-21 17:51pm    
It does compile: https://onlinegdb.com/SkUElHydO
(I made no changes other than removing all those blank lines.)
If you want us to help with your compiler errors, we need the following information:

1. The exact error message that the compiler issues. Make sure to exactly copy/paste the full text. It might also help if you tell us which compiler you are using, and which C++ standard (e.g. GCC/C++14).

2. The line of code that the compiler error indicates, plus a few lines above that (typically just 1 line above is sufficient, but it doesn't hurt to post the code up to the preceding opening bracket '{' ). As OriginalGriff stated in his solution, it helps a lot if the code is properly formatted. There are many editors that support code formatting - in fact, most do, e. g. Notepad++, Sublime, or VSCode. If you don't already use such an editor, I strongly suggest you should start using one: it will greatly help you with your coding efforts!

3. The declaration of any types and variables used in the code you posted, so we can understand what that code does.

4. Maybe a short explanation of what that line of code is supposed to do.

If there is more than one error, check the output and post only the very first error. Depending on the kind of error, it is possible that the following errors are merely side effects of the first.
 
Share this answer
 

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