Skip to content

Instantly share code, notes, and snippets.

@SproutSeeds
Created July 3, 2020 00:59
Show Gist options
  • Save SproutSeeds/e2d77ed5767c1a358e1b1756ff76c4fd to your computer and use it in GitHub Desktop.
Save SproutSeeds/e2d77ed5767c1a358e1b1756ff76c4fd 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 = (struct Song*)calloc(DATABASE_SIZE, sizeof(struct Song));
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 )
{
//cout << token << endl;
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){
char userConfirmation = 'n';
int size = 0;
do{
cout << "What is the Song Title?" << endl;
cin >> songArray[size - 1].title;
cout << "What is the artist name?" << endl;
cin >> songArray[size - 1].artist;
cout << "How many minutes is the song?" << endl;
cin >> songArray[size - 1].minutes;
cout << "How many seconds is the song?" << endl;
cin >> songArray[size - 1].seconds;
cout << "What is the album title?" << endl;
cin >> songArray[size - 1].album;
cout << "You Entered..." << endl;
cout << "Title: " << songArray[size - 1].title << endl;
cout << "Artist: " << songArray[size - 1].artist << endl;
cout << "Minutes: " << songArray[size - 1].minutes << endl;
cout << "Seconds: " << songArray[size - 1].seconds << endl;
cout << "Album: " << songArray[size - 1].album << endl;
cout << "Is this correct? y or n" << endl;
cin >> userConfirmation;
} while(userConfirmation == 'n');
}
void displayAllSongs(struct Song * songArray){
cout << songArray[0].title << endl;
}
void removeSong(struct Song * songArray){
cout << songArray[0].title << endl;
}
void artistSearch(struct Song * songArray){
cout << songArray[0].title << endl;
}
int main(){
struct Song* myArray;
int input = 0;
int* size = 0;
myArray = load_database(size);
cout << "size: " << size << endl;
//cout << "size of array: " << sizeof(myArray) << endl;
//cout << "size of first place of array: " << sizeof(myArray[0]) << endl;
//int numberOfSongs = sizeof(&myArray) / sizeof(&myArray[0]);
//cout << "the size is: " << numberOfSongs << endl;
while(input != 5){
input = showMenu();
if(input == 1){
addSong(myArray);
}else if(input == 2){
displayAllSongs(myArray);
}else if(input == 3){
removeSong(myArray);
}else if(input == 4){
artistSearch(myArray);
}
}
//cout << myArray[1].title << endl;
free(myArray);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment