#include #include #include #include "song.h" using namespace std; // show all songs in a vector of songs void ShowSongs (const vector & all) { int num = all.size(); for (int cnt = 0; cnt < num; cnt++) { cout << all[cnt] << endl; } return; } // ask yes no question - not allowing to continue until y/Y/n/N entered // returns a lower case y or n (always! nothing else) char askYN (const string & prompt) { char answ; // (loop until they give us a valid answer to the question) do { // ask for and get answer cout << prompt << " (y or n): " << flush; cin >> answ; // force all to lower case to simplify while test below if (answ == 'Y') answ = 'y'; if (answ == 'N') answ = 'n'; } while ((answ != 'n') && (answ != 'y')); return answ; } // MAR istream& mygetline(istream &strIn, string &newVal, char toStop = '\n') { char curr; newVal = ""; curr = getchar(); // skip leading "white space" - new line blank while ((curr == ' ') || (curr == '\n')) { curr = getchar(); } // now get real chars while (curr != toStop) { newVal += curr; curr = getchar(); } return strIn; } int main() { char cont; Song unknown; cout << unknown << endl; Song leaving("Leaving Las Vegas","Sheryl Crow",3,39); cout << leaving << endl; vector songs; // empty cout << " initial song list: " << endl; ShowSongs(songs); cout << endl << "size: " << songs.size() << endl << "Capacity: " << songs.capacity() << endl; do { string newTitle; string newArtist; int newMins; int newSecs; cout << "Enter the song title: " << flush; mygetline(cin,newTitle,'\n'); cout << "Enter the artist: " << flush; mygetline(cin,newArtist,'\n'); cout << "Enter the song time in whole minutes " << flush; cin >> newMins; cout << "Now enter seconds " << flush; cin >> newSecs; Song newSong(newTitle,newArtist,newMins,newSecs); songs.push_back(newSong); cout << endl << "Current Songs: " << endl; ShowSongs(songs); cont = askYN("Do you want to continue adding songs?"); } while (cont == 'y'); // other things cout << endl << "size: " << songs.size() << endl << "Capacity: " << songs.capacity() << endl; cout << "Front: " << songs.front() << endl; cout << "Back: " << songs.back() << endl; songs.pop_back(); ShowSongs(songs); cout << endl << "size: " << songs.size() << endl << "Capacity: " << songs.capacity() << endl; askYN("wait"); vector junk(4,unknown); cout << " junk: " << endl; ShowSongs(junk); vector newjunk(4); // same as explicitly using result of default constructor cout << " newjunk: " << endl; ShowSongs(newjunk); newjunk.pop_back(); cout << endl << "size: " << newjunk.size() << endl << "Capacity: " << newjunk.capacity() << endl; vector cp(songs); cout << "copy: " << endl; ShowSongs(cp); cout << endl << "size: " << cp.size() << endl << "Capacity: " << cp.capacity() << endl; return 0; }