View the step-by-step solution to:

Use loops to validate user input and display an error message if they enter data outside the valid range.

In my C++ code, please add loops to validate data. Use loops to validate user input and display an error message if they enter data outside the valid range. Use a main loop to allow the user to keep selecting different tutorials until they decide to exit the program. Add a menu to your program that allows users to use the various features or exit the program.


For a playlist program, you might have options to manage playlists, manage songs, display playlists, and so forth, or exit the program.


#include

#include

#include

#include

#include

#include

#include

using namespace std;

struct Song {

string artistName;

string songTitle;

  string songGenre;

double songLength;

Song(const string s_title, const string n_artist, const string s_genre, const double s_length)

: songTitle(s_title), artistName(n_artist), songLength(s_length), songGenre(s_genre){}  

};

struct Playlist

{  

std::string name;

  double length;

std::vector songList;

Playlist(const string title):name(title){}

void addSong( Song song ) {

songList.push_back( song );

length += song.songLength;

}

};

Playlist& addSongs(Playlist& play);

std::vector& addNewPlaylist(std::vector& play);

void displayPlaylist(Playlist list);

void displayAllPlaylists(const std::vector& plays);

int exitMenuChoice();

void waitForEnter();

void saveonFile(const std::vector& plays);

// Main Menu

int main()

{

vector toPlay; // at least 1 default element

toPlay.push_back(Playlist("default"));

int choice = 0;

do {

cout << "n***************************************************n"

"* *n"

"* Playlist Manager DJ *n"

"* *n"

"***************************************************n"

"Where would you like to start? nn"

"1. Add Songsn"

"2. Add New Playlistn"

"3. View All Playlists n"

"4. Save All Playlists on filen"

"5. Exitnn"

"Enter Your Choice: ";

cin >> choice;

cin.ignore(1);  

  // Set the numeric output formatting.

  

cout << std::fixed << std::showpoint << std::setprecision(2);

  // Respond to the user's menu selection.

switch(choice) {

case 1: // Add songs to a playlist

addSongs(toPlay.at(0));

break;

case 2:

addNewPlaylist(toPlay);

break;

case 3:

displayAllPlaylists(toPlay);

break;

case 4:

saveonFile(toPlay);

break;

case 5:

cout << "Happy Listening!n";

choice = exitMenuChoice();

break;

default:

break;  

}

while (0 < choice && choice < 5);

waitForEnter();

return 0;

}

void displayPlaylist(Playlist list)

{

cout << "This is Playlist '" << list.name << "':n";

cout<< "title: tt; artist: t; genre:t; duration "<

  for (int i = 0; i < list.songList.size(); i++) {

Song s=list.songList.at(i);

//print song title

cout << s.songTitle << "t" << s.artistName

<< "t" << s.songGenre << "t" << s.songLength

<< 'n';

}

//print total length and average song length of play list

cout << "and the length is : " << list.length<

  double songLengthAvg=list.length/list.songList.size();

cout << "The average song length in this playlist is: " << songLengthAvg<

  

}

Playlist& addSongs(Playlist &play)

{

  

string title;

  

do {

  

int i = 1;

  

cout << "nInsert new song into playlist"

  

" - leave title empty to end insertion."

  

"nEnter title of song# " << i << ": ";

  

getline(cin, title);

  

if(title.empty()) { break; }

  

cout << "Enter artist for this song: ";

  

string artist;

  

getline(cin, artist);

  

  

  

cout << "Enter genre for this song: ";

  

string genre;

  

getline(cin, genre);

  

  

  

cout << "Enter duration for this song: ";

  

double song_duration = 0.0;

  

cin >> song_duration;

  

cin.ignore(1);

  

cout << "nAdding " << genre << " song "" << title

  

<< "" by " << artist << ". Running time: "

  

<< song_duration << " mins n";

  

i++;

  

Song song(title, artist, genre, song_duration);

  

play.addSong(song);

  

while (not title.empty());

  

return play;

  

}

vector& addNewPlaylist(vector& play)

{

  

cout << "nEnter Playlist Name: ";

  

string n_playlist;//read name of the list

  

getline(std::cin, n_playlist);

  

Playlist just_created(n_playlist);

  

cout << "nYour new playlist "<< n_playlist << " has been added! nn";

  

char option = 'n';

  

do {

  

cout << "Would you like to add songs to your new playlist "

  

<< n_playlist <<"? (Y/N) ";

  

cin >> option;//read user option

  

cin.ignore(1);

  

option =toupper(option);

  

while (option not_eq 'Y' and option not_eq 'N');

  

if(option == 'Y') { addSongs(just_created); }

  

  

  

play.push_back(just_created);//insert new list in vector

  

return play;

  

}

void displayAllPlaylists(const vector& plays)

{

  

for (int i = 0; i < plays.size(); i++)

  

{

  

displayPlaylist(plays.at(i)); //print each play list

  

cout<

  

}

  

}

//Exit Menu

int exitMenuChoice()

{

  

cout << "nnWould you like to exit the program? (Y/N) ";

  

char answ = 'N';

  

cin >> answ;

  

cin.ignore(1);

//Conditional expression

return ((answ == 'Y' || answ == 'y') ? 5 : 3);

  

// if(answ == 'Y' || answ == 'y') {

  

// return 5;

  

// }

  

// return 3; // == show playlists

  

}

void waitForEnter()

{

  

cout << "nPress ENTER to continue...n";

  

cin.ignore(std::numeric_limits::max(), 'n');

  

}

void saveonFile(const std::vector& plays)

{

  

ofstream outfile("allSongs.txt");//create output file

  

for (int i = 0; i < plays.size(); i++)

  

{

  

Playlist list=plays.at(i);

  

for (int j = 0; j < list.songList.size(); j++) {

  

Song s=list.songList.at(j);

  

//print songs on file

  

outfile << s.songTitle << "t" << s.artistName

  

<< "t" << s.songGenre << "t" << s.songLength

  

<< 'n';

  

}

  

}

  

}

Expert Answer

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question