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 106 107
|
//**********************************************************************************
// Song List Program
// This program inputs information from a file, allows editing to take place, then
// outputs the edited list to the input file.
// Casey Ferguson CSC 210 July 26, 2011
//**********************************************************************************
#include <iostream>
#include <string>
#include <fstream>
#include "Song.h"
using namespace std;
int CheckLibraryLength(ifstream&); // Initializes the library file length check function
void ReadSongInfo(ifstream&, Song&); // Initializes the function meant to read in each object in the array
void TrimSpaces(string&); // Initializes whitespace trimmer function
const int MAX_SONGS = 200; // Sets max song library size at 200
int main()
{
ifstream inFile; // Initializes file operations
ofstream outFile;
string filename; // Initializes filename variable
cout << "Song Library File Name: "; // Prompts for file name
cin >> filename;
inFile.open(filename.c_str()); // Opens the file
outFile.open(filename.c_str());
if (!inFile || !outFile) // Checks the state of the file
{
cout << "Error opening library file.";
return 1;
}
cout << "There are currently " << CheckLibraryLength(inFile)
<< " songs in the library, out of a maximum of " << MAX_SONGS << " songs."; // Outputs how many songs there are and the max number allowed
Song library[MAX_SONGS];
for (int count = 0; count < MAX_SONGS; count++) // Loop meant to read in current library
{
ReadSongInfo(inFile, library[count]);
}
int menuChoice; // Initializes menu number choice
while (menuChoice != 4) // Allows operation to repeat until cancelled using option 4
{
cout << "Library Operation Menu" << endl // Lists menu options
<< "1. Add a song" << endl
<< "2. Remove a song" << endl
<< "3. Display song list" << endl
<< "4. Quit" << endl
<< "Enter the number of your option (1-4): ";
cin >> menuChoice;
if (menuChoice == 1) // If 1, meant to allow the addition of a song
{
int position; // Initializes the position variable
position = sizeof(library) + 1; // Defines said variable
string title, artist, album; // Initializes variables
cout << "Enter the Title of the song: "; // Prompts for title
getline(cin, title, '\n'); // Stores line as title
TrimSpaces(title); // Trims any extraneous whitespace characters
cout << "Enter the Artist: "; // Prompt for artist
getline(cin, artist, '\n'); // Stores the line as artist
TrimSpaces(artist); // Trims extraneous whitespace
cout << "Enter the Album: "; // Prompts for album
getline(cin, album, '\n'); // Stores input line as album
TrimSpaces(album); // Trims the extra whitespace
library[position].Song(title, artist, album); // Meant to store title, artist, and album to the position in the array
}
}
inFile.close(); // Closes inFile
outFile.close(); // Closes outFile
return 0; // Proper return code
}
int CheckLibraryLength (ifstream& inFile) // Function for checking the length of the input/output file
{
int count = 0; // Initializes line counter
string dummy; // Initializes dummy string for input
while (inFile) // While the file is not in fail state, get a line and increment the counter
{
getline(inFile, dummy, '\n');
count++;
}
return count; // Return the number of lines (songs)
}
void ReadSongInfo(ifstream& inFile, Song& library) // Meant to read in song info from the file and store it in the array of the variables in the class file
{
string title, artist, album; // Initializes the variables
getline(inFile, title, ':'); // Inputs all data on line up to the first colon to title
getline(inFile, artist, ':'); // Inputs all data on line after first colon to the second colon to artist
getline(inFile, album, '\n'); // Inputs all data on line after the second colon to the end line character to album
TrimSpaces(title); // These run the function to truncate leading and trailing whitespace from the strings
TrimSpaces(artist);
TrimSpaces(album);
library.Song(title, artist, album); // Meant to store the info to the proper position based on the loop that executes it.
}
void TrimSpaces(string& str)
{
size_t startpos = str.find_first_not_of(" \t"); // Finds the first non-whitespace character
size_t endpos = str.find_last_not_of(" \t"); // Finds last non-whitespace character
if(( string::npos == startpos ) || ( string::npos == endpos)) // If the string is empty, returns a null string
{
str = "";
}
else
str = str.substr( startpos, endpos-startpos+1 ); // Otherwise, returns the string from the first non-whitespace character to the last
}
|