Created
July 3, 2020 04:58
-
-
Save SproutSeeds/7671708967ea82b7b158b50fb41f4c66 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
// Professor Notes | |
/* | |
istream cin; //input stream | |
ifstream file; //file input stream | |
*/ | |
/* | |
while(!file.eof()){ | |
file.get(songArray[j].title, STRING_SIZE, ';'); | |
file.ignore(STRING_SIZE, ';'); | |
file.get(songArray[j].artist, STRING_SIZE, ';'); | |
file.ignore(STRING_SIZE, ';'); | |
} | |
*/ | |
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 << endl; | |
cout << "|------------------------------------------------|" << endl; | |
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; | |
cout << "|------------------------------------------------|" << endl; | |
cout << endl; | |
cin >> input; | |
return input; | |
} | |
void addSong(struct Song * songArray, int& size){ | |
char userConfirmation = 'n'; | |
char inputData[STRING_SIZE]; | |
cin.clear(); | |
while(userConfirmation =='n'){ | |
cin.clear(); | |
cin.ignore(); | |
cout << "What is the Song Title?" << endl; | |
cin.get(songArray[size].title, STRING_SIZE, '\n'); | |
cin.clear(); | |
cin.ignore(); | |
cout << "What is the artist name?" << endl; | |
cin.get(songArray[size].artist, STRING_SIZE, '\n'); | |
cin.clear(); | |
cin.ignore(); | |
cout << "How many minutes is the song?" << endl; | |
cin.get(songArray[size].minutes, STRING_SIZE, '\n'); | |
cin.clear(); | |
cin.ignore(); | |
cout << "How many seconds is the song?" << endl; | |
cin.get(songArray[size].seconds, STRING_SIZE, '\n'); | |
cin.clear(); | |
cin.ignore(); | |
cout << "What is the album title?" << endl; | |
cin.get(songArray[size].album, STRING_SIZE, '\n'); | |
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; | |
} | |
// 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'; | |
outfile.close(); | |
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; | |
if(index == 0){ | |
copy(songArray + 1, songArray + size, songArray + 0); | |
}else{ | |
copy(songArray + (index - 1), songArray + size, songArray + index); | |
} | |
size--; | |
ofstream outfile; | |
outfile.open("songs.txt"); | |
// Writes over the entire songs.txt file | |
for(int i = 0; i < size; i++){ | |
outfile << songArray[i].title << ";" << songArray[i].artist << ";" << songArray[i].minutes << ";" << songArray[i].seconds << ";" << songArray[i].album << '\n'; | |
} | |
outfile.close(); | |
} | |
void artistSearch(struct Song * songArray, int size){ | |
char artist[STRING_SIZE]; | |
memset(artist, 0, STRING_SIZE); | |
cin.clear(); | |
cin.ignore(); | |
cout << "What artist's songs are you searching for?" << endl; | |
cin.get(artist, STRING_SIZE, '\n'); | |
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; | |
} | |
} | |
} | |
int main(){ | |
struct Song* myArray; | |
int input = 0; | |
int size = 0; | |
myArray = load_database(size); | |
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); | |
} | |
} | |
delete [] myArray; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment