I need to add loops to validate my data. Add a menu to my program that allows us
ID: 3843717 • Letter: I
Question
I need to add loops to validate my data. Add a menu to my program that allows users to use the various features or exit the program.
I am making a DJ playlist and need to add the following features by adding loops; options to manage playlists, manage songs, display playlists, and so forth, or exit the program.
Below is my code so far for the entire program I have made, I just need help with the loops and adding those features.
#include
#include
using namespace std;
int main()
{
string pName;
string sTitle1, sTitle2, sTitle3;
string sArtist1, sArtist2, sArtist3;
string sGenre1, sGenre2, sGenre3;
float sLength1, sLength2, sLength3;
float totalLength;
float playlistAverage;
cout << "############################################################ ";
cout << " Welcome to the DJ Dance Playlist Manager ";
cout << "############################################################ ";
cout << "Insert a name for this playlist: ";
getline (cin, pName);
cout << "Insert the title of the first song: ";
getline (cin, sTitle1);
cout << "Insert the artist name of the first song: ";
getline (cin, sArtist1);
cout << "Insert the genre of the first song: ";
getline (cin, sGenre1);
cout << "Insert the length of the first song: ";
cin >> sLength1;
fflush(stdin);
cout << " Insert the title of the second song: ";
getline (cin, sTitle2);
cout << "Insert the artist name of the second song: ";
getline (cin, sArtist2);
cout << "Insert the genre of the second song: ";
getline (cin, sGenre2);
cout << "Insert the length of the second song: ";
cin >> sLength2;
fflush(stdin);
cout << " Insert the title of the third song: ";
getline (cin, sTitle3);
cout << "Insert the artist name of the third song: ";
getline (cin, sArtist3);
cout << "Insert the genre of the third song: ";
getline (cin, sGenre3);
cout << "Insert the length of the third song: ";
cin >> sLength3;
cout << "Choose your favorite genre" << endl;
cout << " 1 : " << sGenre1 << endl;
cout << " 2 : " << sGenre2 << endl;
cout << " 3 : " << sGenre3 << endl;
cout << " 4 : Exit program " << endl;
int choice = 0;
cout << "Your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout << "Wise!" << endl;
break;
case 2:
cout << "Rad!" << endl;
break;
case 3:
cout << "Tubular!" << endl;
break;
case 4:
cout << "Exiting program..." << endl;
break;
}
totalLength = sLength1 + sLength2 + sLength3;
playlistAverage = sLength1 + sLength2 + sLength3 / 3;
cout << "Name of the playlist: " << pName << " ";
cout << "1.-------------------------------------- ";
cout << " Title: " << sTitle1 << " ";
cout << " Artist: " << sArtist1 << " ";
cout << " Genre: " << sGenre1 << " ";
cout << " Length: " << sLength1 << " ";
cout << "2.-------------------------------------- ";
cout << " Title: " << sTitle2 << " ";
cout << " Artist: " << sArtist2 << " ";
cout << " Genre: " << sGenre2 << " ";
cout << " Length: " << sLength2 << " ";
cout << "3.-------------------------------------- ";
cout << " Title: " << sTitle3 << " ";
cout << " Artist: " << sArtist3 << " ";
cout << " Genre: " << sGenre3 << " ";
cout << " Length: " << sLength3 << " ";
cout << "The playlist named " << pName << " has a total length of " << totalLength << " minutes.";
cout << "Also, the playlist named " << pName << " has an average length of " << playlistAverage << "minutes";
}
Explanation / Answer
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
string pName;
string sTitle[3],sArtist[3],sGenre[3];
float sLength[3],totalLength,playlistAverage;
int i;
cout << "############################################################ ";
cout << " Welcome to the DJ Dance Playlist Manager ";
cout << "############################################################ ";
cout << "Insert a name for this playlist: ";
getline (cin, pName);
for(i=0;i<3;i++) // loop to input song details
{
cout << "Insert the title of the song: ";
getline (cin, sTitle[i]);
cout << "Insert the artist name of the song: ";
getline (cin, sArtist[i]);
cout << "Insert the genre of the song: ";
getline (cin, sGenre[i]);
cout << "Insert the length of the song: ";
cin >> sLength[i];
fflush(stdin);
}
cout << "Choose your favorite genre" << endl;
for(i=0;i<3;i++)//loop to display all sGenre
{
cout << " "<<(i+1)<<" : " << sGenre[i] << endl;
}
cout << " 4 : Exit program " << endl;
int choice = 0;
cout << "Your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout << "Wise!" << endl;
break;
case 2:
cout << "Rad!" << endl;
break;
case 3:
cout << "Tubular!" << endl;
break;
case 4:
cout << "Exiting program..." << endl;
break;
}
totalLength = sLength[0] + sLength[1] + sLength[2];
playlistAverage = totalLength / 3;
cout << "Name of the playlist: " << pName << " ";
for(i=0;i<3;i++) //Loop to display song details
{
cout << (i+1)<<".-------------------------------------- ";
cout << " Title: " << sTitle[i] << " ";
cout << " Artist: " << sArtist[i] << " ";
cout << " Genre: " << sGenre[i] << " ";
cout << " Length: " << sLength[i] << " ";
}
cout << "The playlist named " << pName << " has a total length of " << totalLength << " minutes.";
cout << "Also, the playlist named " << pName << " has an average length of " << playlistAverage << "minutes";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.