Skip to content

Instantly share code, notes, and snippets.

@SproutSeeds
Created July 3, 2020 02:52
Show Gist options
  • Save SproutSeeds/81d2642ccbaa8a20630e7f2b173072e6 to your computer and use it in GitHub Desktop.
Save SproutSeeds/81d2642ccbaa8a20630e7f2b173072e6 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
const int STRING_SIZE = 256;
const int LINE_SIZE = 256;
const int DATABASE_SIZE = 256;
int addSong(struct Song songArray);
struct Song{
char title[STRING_SIZE];
char artist[STRING_SIZE];
char minutes[STRING_SIZE];
char seconds[STRING_SIZE];
char album[STRING_SIZE];
};
struct Song* load_database(int& arraySize){
struct Song * songArray = new struct Song[DATABASE_SIZE];
char title[STRING_SIZE];
char artist[STRING_SIZE];
char minutes[STRING_SIZE];
char seconds[STRING_SIZE];
char album[STRING_SIZE];
char line[STRING_SIZE];
char* s = ";";
ifstream file("songs.txt");
if(file.is_open()){
int j = 0;
while(file.getline(line, LINE_SIZE)){
char* token;
int i = 0;
token = strtok(line, s);
while( token != NULL )
{
if(i == 0){
strcpy(songArray[j].title, token);
}else if (i == 1){
strcpy(songArray[j].artist, token);
}else if (i == 2){
strcpy(songArray[j].minutes, token);
}else if (i == 3){
strcpy(songArray[j].seconds, token);
}else if (i == 4){
strcpy(songArray[j].album, token);
}
i++;
token = strtok(NULL, s);
}
j++;
arraySize++;
}
file.close();
}
else cout << "unable to open file." << endl;
return songArray;
}
int showMenu(){
int input;
cout << "Enter 1 to input a new song.." << endl;
cout << "Enter 2 to display information from all songs." << endl;
cout << "Enter 3 to remove a song by index." << endl;
cout << "Enter 4 to search for songs by a specific artist" << endl;
cout << "Enter 5 to quit." << endl;
cin >> input;
return input;
}
void addSong(struct Song * songArray, int& size){
char userConfirmation = 'n';
do{
cout << "What is the Song Title?" << endl;
cin >> songArray[size].title;
cout << "What is the artist name?" << endl;
cin >> songArray[size].artist;
cout << "How many minutes is the song?" << endl;
cin >> songArray[size].minutes;
cout << "How many seconds is the song?" << endl;
cin >> songArray[size].seconds;
cout << "What is the album title?" << endl;
cin >> songArray[size].album;
cout << "You Entered..." << endl;
cout << "Title: " << songArray[size].title << endl;
cout << "Artist: " << songArray[size].artist << endl;
cout << "Minutes: " << songArray[size].minutes << endl;
cout << "Seconds: " << songArray[size].seconds << endl;
cout << "Album: " << songArray[size].album << endl;
cout << "Is this correct? y or n" << endl;
cin >> userConfirmation;
} while(userConfirmation == 'n');
// Append songs.txt
ofstream outfile;
outfile.open("songs.txt", ios_base::app);
outfile << songArray[size].title << ";" << songArray[size].artist << ";" << songArray[size].minutes << ";" << songArray[size].seconds << ";" << songArray[size].album << '\n';
size++;
}
void displayAllSongs(struct Song * songArray, int size){
for(int i = 0; i < size; i++){
cout << "[" << i << "]" << endl;
cout << " " << "Title: " << songArray[i].title << endl;
cout << " " << "Artist: " << songArray[i].artist << endl;
cout << " " << "Minutes: " << songArray[i].minutes << endl;
cout << " " << "Seconds: " << songArray[i].seconds << endl;
cout << " " << "Album: " << songArray[i].album << endl;
}
}
void removeSong(struct Song * songArray, int& size){
int index;
cout << "What Song index would you like to remove?" << endl;
cin >> index;
copy(songArray + (index - 1), songArray + size, songArray + index);
size--;
}
void artistSearch(struct Song * songArray, int size){
char artist[STRING_SIZE];
memset(artist, 0, STRING_SIZE);
cout << "What artist's songs are you searching for?" << endl;
cin >> artist;
for(int i = 0; i < size; i++){
if(strcmp(artist, songArray[i].artist) == 0){
cout << "[" << i << "]" << endl;
cout << " " << "Title: " << songArray[i].title << endl;
cout << " " << "Artist: " << songArray[i].artist << endl;
cout << " " << "Minutes: " << songArray[i].minutes << endl;
cout << " " << "Seconds: " << songArray[i].seconds << endl;
cout << " " << "Album: " << songArray[i].album << endl;
}
}
}
void writeToFile(struct Song * songArray){
// write array to songs.txt
}
int main(){
struct Song* myArray;
int input = 0;
int size = 0;
myArray = load_database(size);
cout << "size: " << size << endl;
while(input != 5){
input = showMenu();
if(input == 1){
addSong(myArray, size);
}else if(input == 2){
displayAllSongs(myArray, size);
}else if(input == 3){
removeSong(myArray, size);
}else if(input == 4){
artistSearch(myArray, size);
}
}
writeToFile(myArray);
cout << "size: " << size << endl;
delete [] myArray;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment