1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#include "Song.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
// Function Prototypes Declared
void whiteSpaceRemover(Song songList[], int lineCount); // Called to trim the whitespace from a string
string equalityTestPrep(string str); // Removes spaces and converts to lowercase to facilitate string comparisons
// Main function
int main() {
// Variable declarations
string artistSearch, songTitle, artistName, albumName;
int lineCount = 0, count = 0;
int token = 0, token2 = 0, token3 = 0;
// Opens input stream for the file
ifstream songFile;
songFile.open("song.in");
// Cycles through the file to evaluate how large an array is needed
while (getline(songFile, artistSearch)) {
lineCount++;
}
artistSearch = "";
// Closes file to be re-opened to reset the line reader
songFile.close();
songFile.open("song.in");
Song songList[lineCount];
string songs[lineCount];
// Stores each line of the file into an array of strings
while (songFile) {
getline(songFile, songs[count]);
count++;
}
// Breaks up and assigns the string values from songs[] to the songList type
for (int i = 0; i < lineCount; i++) {
token = songs[i].find(':');
if(token != string::npos)
songList[i].SetSongTitle(songs[i].substr(0, token));
token2 = songs[i].substr(token + 1, songs[i].length() - 1).find(':') + token;
if(token2 != string::npos)
songList[i].SetArtistName(songs[i].substr(token + 1, token2 - token));
token3 = songs[i].length() - 1;
if(token3 != string::npos)
songList[i].SetAlbumName(songs[i].substr(token2 + 1, token3 - token2));
}
songFile.close();
// Prompts the user for the artist by which to search the library for
cout << "Enter the artist to search for: ";
getline(cin, artistSearch);
cout << endl << endl;
// Calls the function to remove excess whitespace from the strings within the songList array
whiteSpaceRemover(songList, lineCount);
// Tests for equality and outputs all information related to the artist
for(int i = 0; i < lineCount; i++) {
songTitle = songList[i].GetArtistName();
if(equalityTestPrep(songTitle) == equalityTestPrep(artistSearch)) {
artistName = songList[i].GetArtistName();
albumName = songList[i].GetAlbumName();
cout << songTitle << " : " << artistName << " : " << albumName << endl;
}
}
}
// Trims the whitespace from the songList[] strings for prettier output and better organization
void whiteSpaceRemover(Song songList[], int lineCount) {
int token;
string songTitle, artistName, albumName;
for(int i = 0; i < lineCount; i++) {
songTitle = songList[i].GetSongTitle();
token = songTitle.find('\t');
if(token != string::npos)
songList[i].SetSongTitle(songTitle.substr(0, token));
artistName = songList[i].GetArtistName();
token = artistName.find_first_not_of('\t');
if(token != string::npos) {
songList[i].SetArtistName(artistName.substr(token, artistName.find('\t') - token));
songList[i].SetArtistName(artistName.substr(0, artistName.find('\t')));
}
albumName = songList[i].GetAlbumName();
token = albumName.find('\t');
if(token != string::npos)
songList[i].SetAlbumName(albumName.substr(1, token));
}
}
|