You work for a disc jockey who has asked you to obtain a list of the top 1000 must listen to songs1 and write a program that allows her to list the songs according to Theme, Title, Artist, Year. In addition, from the list she can select a particular song to Be played on “Spotify” (NOTE: the "playing on Spotify" is sketchy and may not work correctly on your computer but as long as the code to do so is correct then you will get full credit). To be considered correct, your program will need to implement several functions described nex
The expected output is this. correct output The output I"m getting is this. error output
note. I'm gathering my 1000 song info from a separate file obviously. Below is the code.
#include <fstream>
#include <string.h>
#include <windows.h>
#include <conio.h>
using namespace std;
using std::cout;
using std::endl;
using std::cin;
enum COLUMNINDEX {
THEME, TITLE, ARTIST, YEAR, SPOTIFYURL
}; // Enumeration declaration
COLUMNINDEX GetColumnIndex(string str)
{
// Comparing user entered string to see what data to prompt for next
COLUMNINDEX temp;
if (str.compare("Theme") == 0)
temp = THEME;
else if (str.compare("Title") == 0)
temp = TITLE;
else if (str.compare("Artist") == 0)
temp = ARTIST;
else if (str.compare("Year") == 0)
temp = YEAR;
else if (str.compare("Spotifyurl") == 0)
temp = SPOTIFYURL;
return temp; // Returning Enum
}
void getColumnValue(std::ifstream &data, int check, string str2)
{
string temp1, temp2, temp3, temp4, temp5;
int flag, i;
do {
flag = 0;
getline(data, temp1, ',');
if ((check == 0) && (temp1.compare(str2) == 0)) // checking if the
search is in column "Theme"
flag = 1;
getline(data, temp2, ',');
if ((check == 1) && (temp2.compare(str2) == 0)) // checking if the
search is in column "Title"
flag = 1;
getline(data, temp3, ',');
if ((check == 2) && (temp3.compare(str2) == 0)) // checking if the
search is in column "Artist"
flag = 1;
getline(data, temp4, ',');
if ((check == 3) && (temp4.compare(str2) == 0)) // checking if the
search is in column "Year"
flag = 1;
getline(data, temp5);
if (flag == 1)
{
for (i = 0; i<temp1.length(); i++)
temp1[i] = toupper(temp1[i]); // converting file info to upper
case for display
for (i = 0; i<temp2.length(); i++)
temp2[i] = toupper(temp2[i]); // converting file info to upper
case for display
for (i = 0; i<temp3.length(); i++)
temp3[i] = toupper(temp3[i]); // converting file info to upper
case for display
if (check == 0)
{
cout << "Title: " << temp2 << endl;
cout << "Artist: " << temp3 << endl; // Displaying the record
cout << "Year: " << temp4 << endl;
}
else if (check == 1)
{
cout << "Theme: " << temp1 << endl;
cout << "Artist: " << temp3 << endl; // Displaying the record
cout << "Year: " << temp4 << endl;
}
else if (check == 2)
{
cout << "Theme: " << temp1 << endl;
cout << "Title: " << temp2 << endl; // Displaying the record
cout << "Year: " << temp4 << endl;
}
else if (check == 4)
{
cout << "Theme: " << temp1 << endl;
cout << "Title: " << temp2 << endl; // Displaying the record
cout << "Artist: " << temp3 << endl;
}
cout << endl;
}
} while (flag == 0 && !data.eof()); // Loop until required data is found
or until End Of File
}
string getSpotifyURL(int recordNumber, std::ifstream &data)
{
string temp;
int i = 0;
getline(data, temp);
while (i <= recordNumber) // loop until required record number is reached
{
getline(data, temp, ',');
getline(data, temp, ',');
getline(data, temp, ',');
getline(data, temp, ',');
getline(data, temp); // temp contains the URL
i++;
}
return temp; // returning the URL
}
void LaunchSpotify(string URL)
{
ShellExecuteA(NULL, NULL, URL.c_str(), NULL, NULL, SW_SHOWNORMAL); //
launching Spotify in the web browser
}
int main()
{
cout << "-----------------------------------------------------------------------------" << endl;
cout << "ITCS 2530 - Programming Assignment for week #6" << endl;
cout << "-----------------------------------------------------------------------------" << endl;
std::ifstream data("1000songs.csv"); // opening the data file
int numRecords, i;
string str1, str2, s;
cout << "Please enter the column to search (Theme, Title, Artist or Year):
";
cin >> str1; // str1 contains the column name on which the search is to
occur
cout << endl;
COLUMNINDEX column;
column = GetColumnIndex(str1); // column variable stores the column index on
which the search is to occur
if (column == 0)
cout << "Please enter the value in the \"THEME\" column to locate: ";
else if (column == 1)
cout << "Please enter the value in the \"TITLE\" column to locate: ";
else if (column == 2)
cout << "Please enter the value in the \"ARTIST\" column to locate: ";
else if (column == 3)
cout << "Please enter the value in the \"YEAR\" column to locate: ";
cin >> str2; // Stores the name of Theme/Title/Artist/Year for which the
search will occur
cout << endl << "Please enter the maximum number of records to display per
page (1 to 100) : ";
cin >> numRecords; // Stores number of records to be displayed per page
for (i = 0; i<str1.length(); i++)
str1[i] = toupper(str1[i]);
// for(i=0 ; i<str2.length() ; i++)
// s[i]=toupper(str2[i]);
cout << endl << "...Searching column " << str1 << " for the item \"" << str2
<< "\"." << endl << endl;
int count, total = 0;
string temp;
char ch;
while (!data.eof()) // loop until end of file
{
count = 0;
while (count < numRecords) // loop until records are displayed
{
if (str1.compare("THEME") == 0)
{
cout << "Rec No. " << count << endl;
getColumnValue(data, 0, str2); // function call to search records with string "str2" in column "Theme"
count++;
}
else if (str1.compare("TITLE") == 0)
{
cout << "Rec No. " << count << endl;
getColumnValue(data, 1, str2); // function call to search records with string "str2" in column "Title"
count++;
}
else if (str1.compare("ARTIST") == 0)
{
cout << "Rec No. " << count << endl;
getColumnValue(data, 2, str2); // function call to search records with string "str2" in column "Artist"
count++;
}
else if (str1.compare("YEAR") == 0)
{
cout << "Rec No. " << count << endl;
getColumnValue(data, 3, str2); // function call to search records with string "str2" in column "Year"
count++;
}
}
total = total + count; // counting total number of records displayed
cout << count << " record(s) found."; // prints how many records were found in the lastest search
cout << endl << "Press any key to continue searching or 'x' to stop.." << endl;
cin >> ch; // stores the character which is used to check if searching needs to stop
if (ch == 'x' || ch == 'X') // checking if searching needs to stop
{
data.close(); // closing the 1000songs.csv file
cout << "Search Complete. " << total << " record(s) that match search criteria";
cout << endl << "Attempt to go to \"Spotify\" on exit? (y/n):";
cin >> ch;
if (ch == 'y' || ch == 'Y') //checking if attempt needs to be made to go to spotify
{
string URL;
int num;
cout << "Please enter a record number to Spotify: ";
cin >> num; // num stores the record number which needs to be played on spotify
std::ifstream data("1000songs.csv"); // opening the 1000songs.csv file
URL = getSpotifyURL(num, data); // URL stores the url of the song to be played
if (URL.compare("") == 0)
cout << "No Spotify URL available for this record";
else
LaunchSpotify(URL); // calls the function which attempts to launch spotify
}
break; // program controls comes out of the loop
}
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire