Skip to content

Instantly share code, notes, and snippets.

@NickersF
Created February 22, 2016 06:05
Show Gist options
  • Save NickersF/2a6d047ca29a0ca8a4b3 to your computer and use it in GitHub Desktop.
Save NickersF/2a6d047ca29a0ca8a4b3 to your computer and use it in GitHub Desktop.
proj2 source
// Author: Nicholas Fazzolari
// Date: 1/22/16
// Assignment: Song data base application
// Sources: Text Book, cplusplus.com
#include <iostream>
#include <iomanip>
#include <cstring>
#include <fstream>
using namespace std;
const int CHAR_ARR_MAX = 256;
// struct to hold the song data components (make this a class)
struct Song
{
char title[CHAR_ARR_MAX];
char artist[CHAR_ARR_MAX];
char length[CHAR_ARR_MAX];
char album[CHAR_ARR_MAX];
};
// function prototypes (move to header file for class based version)
// file operations
void loadFile(const char inputFile[], Song list[], int& listLength);
void saveFile(const char inputFile[], const Song list[], int listLength);
// user functions
void usrMenu(char selection, Song list[], int& listLength);
void printSongs(const Song list[], int listLength);
bool searchSongsByAlbum(const char search[], const Song list[], int listLength);
bool searchSongsByArtist(const char search[], const Song list[], int listLength);
void removeSong(Song list[], int& listLength);
// utility functions
void usrEntries(const Song& songEntry, Song list[], int& listLength);
void readInput(Song& songEntry);
void getInput(const char inputMsg[], char input[], int arr_size);
int main() {
// locals
Song listLength[CHAR_ARR_MAX];
char inFile[CHAR_ARR_MAX];
int songIds;
char userSelection;
cout << endl << "Enter path/name to datafile: ";
cin >> inFile;
loadFile(inFile, listLength, songIds);
// user interface
do {
cout << endl
<< "MAKE A SELECTION: " << endl
<< "[1] Print Songs" << endl
<< "[2] Add Entry" << endl
<< "[3] Remove Song (use index number)" << endl
<< "[4] Search by Artist" << endl
<< "[6] Search by Album" << endl
<< "(When done enter 'q' to quit)" << endl
<< "Please make a menu selection: ";
cin >> userSelection;
cin.ignore(100, '\n');
// parse the user input
usrMenu(userSelection, listLength, songIds);
} while (userSelection != 'q');
// call saveFile function
saveFile(inFile, listLength, songIds);
return 0;
}
// this function loads the file for manipulation
void loadFile(const char inputFile[], Song list[], int& listLength) {
// locals
ifstream inFile;
Song songEntry;
char fileTitle[CHAR_ARR_MAX];
char fileArtist[CHAR_ARR_MAX];
char fileLength[CHAR_ARR_MAX];
char fileAlbum[CHAR_ARR_MAX];
// open user file
inFile.open(inputFile);
if (!inFile) {
inFile.clear();
cout << "invalid input file" << endl;
return;
}
inFile.get(fileTitle, CHAR_ARR_MAX, '-');
while (!inFile.eof()) {
inFile.get();
inFile.get(fileArtist, CHAR_ARR_MAX, '-');
inFile.get();
inFile.get(fileLength, CHAR_ARR_MAX, '-');
inFile.get();
inFile.get(fileAlbum, CHAR_ARR_MAX, '-');
inFile.get();
inFile.ignore(1000, '\n');
// add data to struct
strcpy(songEntry.title, fileTitle);
strcpy(songEntry.artist, fileArtist);
strcpy(songEntry.length, fileLength);
strcpy(songEntry.album, fileAlbum);
usrEntries(songEntry, list, listLength);
inFile.get(fileTitle, CHAR_ARR_MAX, '-');
}
inFile.close();
}
// this function saves the file
void saveFile(const char inputFile[], const Song list[], int listLength) {
// locals
ofstream outFile;
int i;
outFile.open(inputFile);
// error handling for data files
if (!outFile) {
cout << "invalid file" << endl;
outFile.clear();
return;
}
for (i = 0; i < listLength; i++) {
outFile << list[i].title << "-" << list[i].artist << "-" << list[i].length << "-" << list[i].album << "-" << endl;
}
outFile.close();
}
// this function handles the UI logic
void usrMenu(char menuChoice, Song list[], int& listLength) {
// locals
Song newEntry;
char search[CHAR_ARR_MAX];
// switch for user selections
switch (menuChoice) {
case '1':
printSongs(list, listLength);
break;
case '2':
readInput(newEntry);
usrEntries(newEntry, list, listLength);
printSongs(list, listLength);
break;
case '3':
removeSong(list, listLength);
break;
case '4':
getInput("Enter the artist name: ", search, CHAR_ARR_MAX);
searchSongsByArtist(search, list, listLength);
break;
case '5':
getInput("Enter the album name: ", search, CHAR_ARR_MAX);
searchSongsByAlbum(search, list, listLength);
break;
case 'q':
cout << "Shutting down!" << endl;
break;
default:
cout << "Invalid input" << endl;
break;
}
}
// this functions prints out the songs
void printSongs(const Song list[], int listLength) {
// locals
int i;
cout << setw(17) << left << "Title" << setw(22) << left << "Artist" << setw(10) << left << "Length" << setw(22) << left << "Album" << endl;
for (i = 0; i < listLength; i++) {
if (i < 10) {
cout << i << setw(16) << right << list[i].title << setw(22) << right << list[i].artist << setw(10) << right << list[i].length << setw(22) << right << list[i].album << endl;
}
else {
cout << i << setw(17) << right << list[i].title << setw(22) << right << list[i].artist << setw(10) << right << list[i].length << setw(22) << right << list[i].album << endl;
}
}
cout << "There are " << listLength << " in the database." << endl;
}
// this functions searches for the songs by album
bool searchSongsByAlbum(const char searchArr[], const Song list[], int listLength) {
// locals
int i;
bool searchGood = false;
cout << "Search songs by Album title: " << endl;
cout << setw(20) << "Title" << setw(20) << "Artist" << setw(20) << "Length" << setw(20) << "Album" << endl;
// the search algo
for (i = 0; i < listLength; i++) {
if (strcmp(searchArr, list[i].album) == 0) {
cout << i << setw(20) << list[i].title << setw(20) << list[i].artist << setw(20) << list[i].length << setw(20) << list[i].album << endl;
searchGood = true;
}
}
if (searchGood == false) {
cout << "Could not find album in your database, try again." << endl;
}
return searchGood;
}
// this function searches for the songs by artist
bool searchSongsByArtist(const char searchArr[], const Song list[], int listLength) {
// locals
int i;
bool searchGood = false;
cout << "Search songs by artist: " << endl;
cout << setw(20) << "Title" << setw(20) << "Artist" << setw(20) << "Length" << setw(20) << "Album" << endl;
// the search algo
for (i = 0; i < listLength; i++) {
if (strcmp(searchArr, list[i].artist) == 0) {
cout << i << setw(20) << list[i].title << setw(20) << list[i].artist << setw(20) << list[i].length << setw(20) << list[i].album << endl;
searchGood = true;
}
}
// if the search fails ask:
if (searchGood == false) {
cout << "Could not find artist in your database, try again." << endl;
}
return searchGood;
}
// this functions allows you to remove songs
void removeSong(Song list[], int& listLength) {
// locals
int usrIndex;
cout << "Enter the index of the song you want to delete: " << endl;
cin >> usrIndex;
// handle invalid input range (move to function?)
if (usrIndex < 0 || usrIndex >= listLength) {
cout << "Invalid Input" << endl;
return;
} else {
if (usrIndex == listLength - 1) {
listLength = listLength - 1;
} else {
strcpy(list[usrIndex].title, list[listLength - 1].title);
strcpy(list[usrIndex].artist, list[listLength - 1].artist);
strcpy(list[usrIndex].length, list[listLength - 1].length);
strcpy(list[usrIndex].album, list[listLength - 1].album);
listLength = listLength - 1;
}
}
// show new listLength after deletion
printSongs(list, listLength);
}
// this is the entries function for adding the data to the array struct
void usrEntries(const Song& songEntry, Song list[], int& listLength) {
strcpy(list[listLength].title, songEntry.title);
strcpy(list[listLength].artist, songEntry.artist);
strcpy(list[listLength].length, songEntry.length);
strcpy(list[listLength].album, songEntry.album);
listLength++;
}
// this function reads in the database entries
void readInput(Song& songEntry) {
//variable declaration
char entryTitle[CHAR_ARR_MAX];
char entryArtist[CHAR_ARR_MAX];
char entryLength[CHAR_ARR_MAX];
char entryAlbum[CHAR_ARR_MAX];
getInput("Enter song title: ", entryTitle, CHAR_ARR_MAX);
getInput("Enter song artist: ", entryArtist, CHAR_ARR_MAX);
getInput("Enter song length: ", entryLength, CHAR_ARR_MAX);
getInput("Enter song album: ", entryAlbum, CHAR_ARR_MAX);
// add the data to the struct
strcpy(songEntry.title, entryTitle);
strcpy(songEntry.artist, entryArtist);
strcpy(songEntry.length, entryLength);
strcpy(songEntry.album, entryAlbum);
}
// this function defines the new input
void getInput(const char inputMsg[], char input[], int arrLength) {
// print questions for user
cout << endl << inputMsg;
cin.get(input, arrLength, '\n');
while (!cin) {
cin.clear();
cin.ignore(100, '\n');
cout << endl << inputMsg;
cin.get(input, arrLength, '\n');
}
cin.ignore(100, '\n');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment