Please help! I created a DMM program in C++ that stores a file into a doubly linked list. I created a function that loads the file.csv into a linked list which works fine and I need help on writing that file into the same file.. When I compile the store function, it just writes random numbers ex( 0:1875941888 -1 0).
What must Store do?
The store command writes the current records, in the dynamic doubly linked list, to the musicPlayList.csv file. The store will completely overwrite the previous contents in the file.
My code:
----------------------LIST.CPP
void List::load(fstream& fileStream)
{
char delim1 = ',';
char delim2 = ':';
while (!fileStream.eof())
{
Record rec;
getline(fileStream, rec.artist, delim1);
getline(fileStream, rec.album,delim1);
getline(fileStream, rec.song, delim1);
getline(fileStream, rec.genre, delim1);
fileStream >> rec.length.minutes >> delim2;
fileStream >> rec.length.seconds >> delim1;
fileStream >> rec.num_plays >> delim1;
fileStream >> rec.rating;
addEnd(rec);
cout << rec.artist << ' ' << rec.album << ' ' << rec.song <<
' ' << rec.genre << ' ' << rec.length.minutes << ':' << rec.length.seconds
<< ' ' << rec.num_plays << ' ' << rec.rating;
}
cout << '\n';
}
void List::addEnd(Record data)
{
if (m_head == nullptr)
{
m_head = m_tail = new ListNode(data,nullptr,nullptr);
}
else // not empty
{
m_tail = new ListNode(data, nullptr, m_tail);
m_tail->prev->next = m_tail;
}
++m_size;
}
void List::store(fstream& fileStream)
{
Record rec;
ListNode* nodePtr = m_head;
// while(nodePtr->next != nullptr)
// {
// nodePtr = nodePtr->next;
// }
// while (nodePtr->prev != nullptr)
// {
if (nodePtr->next == nullptr)
{
return;
}
fileStream.open("musicPlayList.txt", ios::out);
fileStream << rec.artist << ' ' << rec.album << ' ' << rec.song
<< ' ' << rec.genre << ' ' << rec.length.minutes << ':'
<< rec.length.seconds << ' ' << rec.num_plays << ' '
<< rec.rating;
//nodePtr = nodePtr->prev;
}
------------------------MAIN.CPP
#include <iostream>
#include <string>
#include <fstream>
#include "list.h"
using namespace std;
int main()
{
List myList;
fstream readFile;
readFile.open("musicPlayList.csv", ios::in);
if (readFile.fail())
{
cout << "Error opening file!\n";
exit(1);
}
int menu;
do
{
myList.menu();
//menu = myList.getChoice(1, 11);
cout << "Enter a menu: ";
cin >> menu;
if(menu == 1)
{
myList.load(readFile);
readFile.close();
}
if(menu == 2)
{
myList.store(readFile);
readFile.close();
}
return 0;
}
------------------------ list.h
#ifndef LIST_H
#define LIST_H
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
using namespace std;
struct Duration
{
unsigned int seconds;
unsigned int minutes;
};
struct Record
{
string artist;
string album;
string song;
string genre;
Duration length; // the song length
int num_plays; //times played
int rating; //rating 1-5
};
struct ListNode
{
Record data;
ListNode* next;
ListNode* prev;
ListNode() {};
ListNode(Record data1, ListNode* next1=nullptr, ListNode* prev1=nullptr)
{
data = data1;
next = next1;
prev = prev1;
}
};
class List
{
public:
// Adds an element to the end of the list
void addEnd(Record data);
void menu();
void load(fstream& fileStream);
void store(fstream& fileStream);
// Default constructor to initialize
List();
~List();
private:
ListNode* m_head; // points to the head of the list
ListNode* m_tail; // points to the end of the list
// tracks the number of elements
int m_size;
};
#endif
------------------------- musicPlatList.csv
Taylor Swift,1989,Shake it Off,Pop,3:35,12,3
Disturbed,THE SICKNESS,Down With The Sickness,Hard
Rock,4:39,8,4
Drake,NOTHING WAS THE SAME,Own It,Rap,3:23,3,3
Disturbed,ASYLUM,Asylum,Hard Rock,4:36,2,4
Drake,YOU WELCOME,The Motto,Rap,4:13,7,4
Christina Perri,HEAD OF HEART,Trust,Pop,2:35,3,5
Justin Bieber,PURPOSE,No Sense,Pop,4:12,6,1
Eminem,SHADYXV,Vegas,Rap,3:37,8,3
Adele,25,Remedy,Pop,4:11,24,4
Disturbed,INDESTRUCTIBLE,The Night,Hard Rock,4:46,7,3
Taylor Swift,RED,Stay Stay Stay,Pop,4:42,5,1
Garth Brooks,FRESH HORSES,The Old Stuff,Country,2:57,11,2
Nirvana,NEVERMIND,Come As You Are,Grunge,3:38,5,4
Eminem,8 Mile,Lose Yourself,Rap,5:24,12,5
Five Finger Death Punch,American Capitalist,Back For More,Hard
Rock,2:34,8,4
Disturbed,INDESTRUCTIBLE,Indestructible,Hard Rock,4:38,11,5
Please let me know if you need more information :-
======================
#include <iostream>
#include "list.h"
#include <fstream>
using namespace std;
int main()
{
fstream readFile;
readFile.open("musicPlayList.txt", ios::in);
if (readFile.fail())
{
cout << "Error opening file!\n";
exit(1);
}
int menu;
List myList; //<=== This is the right position for your list reference.
do
{
//List myList; <==== it should not be here. when loops
comes back here your list again will become empty or
null.
myList.menu();
cout << "Enter a menu: ";
cin >> menu;
if(menu == 1)
{
myList.load(readFile);
readFile.close();
}
if(menu == 2)
{
myList.store(readFile);
readFile.close();
}
if(menu == 3)
{
myList.displayList();
}
} while(menu != 99);
return 0;
}
===
list.cpp
===
#include "list.h"
#include <iostream>
#include <fstream>
using namespace std;
List::List()
{
m_head = nullptr;
m_tail = m_head;
m_size = 0;
}
void List::menu()
{
cout << "[ 1 ] Load [ 7 ] Delete\n"
<< "[ 2 ] Store [ 8 ] Insert\n"
<< "[ 3 ] Display [ 9 ] Sort\n"
<< "[ 4 ] Edit [ 10 ] Reverse\n"
<< "[ 5 ] Rate [ 11 ] Shuffle\n"
<< "[ 6 ] Play [ 99 ] Exit\n";
}
void List::load(fstream& fileStream)
{
char delim = ',';
char delim2 = ':';
while(!fileStream.eof())
{
Record rec;
string dummy="";
getline(fileStream, rec.artist, delim);
getline(fileStream, rec.album, delim);
getline(fileStream, rec.song, delim);
getline(fileStream, rec.genre, delim);
fileStream >> rec.length.minutes >> delim2;
fileStream >> rec.length.seconds >> delim;
fileStream >> rec.num_plays >> delim;
fileStream >> rec.rating;
addEnd(rec);
}
}
void List::addEnd(Record data)
{
if (m_head == nullptr)
{
m_head = m_tail = new ListNode(data,nullptr,nullptr);
}
else // not empty
{
m_tail = new ListNode(data, nullptr, m_tail);
m_tail->prev->next = m_tail;
}
// displayList();
++m_size;
}
void List::store(fstream& fileStream)
{
Record rec;
ListNode* nodePtr = m_head;
fileStream.open("musicPlayList.txt", ios::out);
if (nodePtr == nullptr)
{
return;
}
while(nodePtr != nullptr)
{
rec = nodePtr->data;
fileStream << rec.artist << ' ' << rec.album << ' ' << rec.song
<< ' ' << rec.genre << ' ' << rec.length.minutes << ':'
<< rec.length.seconds << ' ' << rec.num_plays << ' '
<< rec.rating; ////Here you must change the ' ' ==> with comma if you want the file to be written with ',' .
nodePtr = nodePtr->next;
}
}
void List::displayList()
{
ListNode* nodePtr = m_head;
if (nodePtr == nullptr)
{
return;
}
while(nodePtr != nullptr)
{
cout << nodePtr->data.artist << ' ' << nodePtr->data.album << ' ' << nodePtr->data.song
<< ' ' << nodePtr->data.genre << ' ' << nodePtr->data.length.minutes << ':'
<< nodePtr->data.length.seconds << ' ' << nodePtr->data.num_plays << ' '
<< nodePtr->data.rating;
nodePtr = nodePtr->next;
}
cout << "\n";
}
List::~List()
{
while(m_tail->prev != nullptr)
{
m_tail = m_tail->prev;
delete m_tail->next;
}
delete m_tail;
m_tail = nullptr;
m_head = nullptr;
}
====
list.h
====
#include <iostream>
#include<string>
using namespace std;
struct Duration
{
unsigned int seconds;
unsigned int minutes;
};
struct Record
{
string artist;
string album;
string song;
string genre;
Duration length; // the song length
int num_plays; //times played
int rating; //rating 1-5
};
struct ListNode
{
Record data;
ListNode* next;
ListNode* prev;
ListNode() {};
ListNode(Record data1, ListNode* next1=nullptr, ListNode* prev1=nullptr)
{
data = data1;
next = next1;
prev = prev1;
}
};
class List
{
public:
// Adds an element to the end of the list
void addEnd(Record data);
void menu();
void load(fstream& fileStream);
void store(fstream& fileStream);
void displayList();
// void searchList();
// Default constructor to initialize
List();
~List();
private:
ListNode* m_head; // points to the head of the list
ListNode* m_tail; // points to the end of the list
// tracks the number of elements
// currently present in the list
int m_size;
};
====
OUTPUT:-
====
[ 1 ] Load [ 7 ] Delete
[ 2 ] Store [ 8 ] Insert
[ 3 ] Display [ 9 ] Sort
[ 4 ] Edit [ 10 ] Reverse
[ 5 ] Rate [ 11 ] Shuffle
[ 6 ] Play [ 99 ] Exit
Enter a menu: 1
[ 1 ] Load [ 7 ] Delete
[ 2 ] Store [ 8 ] Insert
[ 3 ] Display [ 9 ] Sort
[ 4 ] Edit [ 10 ] Reverse
[ 5 ] Rate [ 11 ] Shuffle
[ 6 ] Play [ 99 ] Exit
Enter a menu: 3
Taylor Swift 1989 Shake it Off Pop 3:35 12 3
Disturbed THE SICKNESS Down With The Sickness Hard Rock 4:39 8
4
Drake NOTHING WAS THE SAME Own It Rap 3:23 3 3
Disturbed ASYLUM Asylum Hard Rock 4:36 2 4
Drake YOU WELCOME The Motto Rap 4:13 7 4
Christina Perri HEAD OF HEART Trust Pop 2:35 3 5
Justin Bieber PURPOSE No Sense Pop 4:12 6 1
Eminem SHADYXV Vegas Rap 3:37 8 3
Adele 25 Remedy Pop 4:11 24 4
Disturbed INDESTRUCTIBLE The Night Hard Rock 4:46 7 3
Taylor Swift RED Stay Stay Stay Pop 4:42 5 1
Garth Brooks FRESH HORSES The Old Stuff Country 2:57 11 2
Nirvana NEVERMIND Come As You Are Grunge 3:38 5 4
Eminem 8 Mile Lose Yourself Rap 5:24 12 5
Five Finger Death Punch American Capitalist Back For More Hard Rock
2:34 8 4
Disturbed INDESTRUCTIBLE Indestructible Hard Rock 4:38 11 5
[ 1 ] Load [ 7 ] Delete
[ 2 ] Store [ 8 ] Insert
[ 3 ] Display [ 9 ] Sort
[ 4 ] Edit [ 10 ] Reverse
[ 5 ] Rate [ 11 ] Shuffle
[ 6 ] Play [ 99 ] Exit
Enter a menu: 2
[ 1 ] Load [ 7 ] Delete
[ 2 ] Store [ 8 ] Insert
[ 3 ] Display [ 9 ] Sort
[ 4 ] Edit [ 10 ] Reverse
[ 5 ] Rate [ 11 ] Shuffle
[ 6 ] Play [ 99 ] Exit
Enter a menu: 99
===

==

==
Please let me know if you need more information.
==
Thanks
Please help! I created a DMM program in C++ that stores a file into a doubly...
In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode { public: T data; ListNode<T>* next; ListNode(T data) { this->data = data;...
Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...
Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...
I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I have right now. ************city.h**************** #ifndef city_h #define city_h #include <string> using namespace std; class City{ public: City () { name = "N/A"; population = 0; } City (string nm, unsigned int pop){ name = nm; population = pop; } void setName (string name) { this -> name = name; } void setPopulation (unsigned int population){ this -> population = population; } string getName() const...
I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...
I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words); int main() { // Declaring variables std::map<std::string,int> words; //defines an input stream for the data file ifstream dataIn; string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...
I am reading a text file and trying to store the information into an array so I can use this in different parts of my program. So a dynamic array. So far I have been able to retrieve the information from the text file and organize it by category (User name,User ID, User password) I am having troubles allocating an array and storing the information correctly. Since I have different data types (int, string) do I need to create a...
The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...
I am having trouble trying to output my file Lab13.txt. It will
say that everything is correct but won't output what is in the
file. Please Help
Write a program that will input data from the file
Lab13.txt(downloadable
file);
a name, telephone number, and email
address.
Store the data in a simple local array to the main
module, then sort the array by the names. You should have several
functions that pass data by reference.
Hint: make your array large...
can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...