Write your question here.
Hi,
Im tryin to write this code. The music library suppused to contain the following information for each song:
id (sequence of numbers and/or letters, without spaces)
title (may contain spaces in it)
artist (may contain spaces in it)
size (in Megabytes, could have fractional parts)
plays (number of times the song has been played, a whole number)
rating (a value between 1 and 5, the number of stars in the rating, a whole number)
and the output should be like :
Menu
1. Display Songs sorted by title
2. Display Songs sorted by rating
3. Lookup title and artist by ID
4. Lookup ID by title and artist
5. Quit the Program
Enter your choice: 1
Happy Pharrell Williams 3.9 MB 15 4
Let It Go Idina Menzel 4.8 MB 99 5
My Heart Will Go On Celine Dion 5.4 MB 9 5
You've Got a Friend in Me Randy Newman 4.2 MB 6 2
but im keep getting wierd things when i input my number here s my code:
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
struct music
{
string id;
string title;
string artist;
double s;
int plays;
int rating;
};
void print(struct music x) //function to print the details of a song
{
cout << x.title << " " << x.artist << " " << x.s << " MB " << x.plays << " " << x.rating << " " << endl;
}
int main()
{
ifstream fin;
fin.open("library.txt");
struct music a[100];
int n = 0;
while (n<100)
{
fin >> a[n].id;
fin >> a[n].title;
fin >> a[n].artist;
fin >> a[n].s;
fin >> a[n].plays;
fin >> a[n].rating;
if (a[n].id != "")
n++;
else
break;
}
int ch, j, i, flag;
ch = 0;
string t, id, ar;
while (ch != 5)
{
cout << "Menu" << endl << endl;
cout << "1. Display songs sorted by title" << endl;
cout << "2. Display songs sorted by rating" << endl;
cout << "3. Lookup title and artist by id" << endl;
cout << "4. Lookup id by title and artist" << endl;
cout << "5. Quit the program" << endl;
cout << "Enter your choice : ";
cin >> ch;
switch (ch)
{
case 1:
for (i = 0;i<n - 1;i++)
{
for (j = 0;j<n - i - 1;j++)
{
if (a[j].title>a[j + 1].title) //sorting all the songs by title
{
struct music temp;
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
for (i = 0;i<n;i++)
{
print(a[i]);
}
break;
case 2:
for (i = 0;i<n - 1;i++)
{
for (j = 0;j<n - i - 1;j++)
{
if (a[j].rating>a[j + 1].rating) //sorting all the songs by rating
{
struct music temp;
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
for (i = 0;i<n;i++)
{
print(a[i]);
}
break;
case 3:
cout << "Enter id : ";
cin >> id;
flag = 0;
for (j = 0;j<n;j++) //checking all the songs for input id and printing the required title and author
{
if (a[j].id == id)
{
cout << "The song with id " << id << " is " << a[j].title << " by " << a[j].artist << endl;
flag = 1;
}
}
if (flag == 0)
cout << "Id not found!" << endl;
break;
case 4:
cout << "Enter title : ";
cin >> t;
cout << "Enter artist : ";
cin >> ar;
flag = 0;
for (j = 0;j<n;j++) //checking all the songs for input author and title and printing the required id
{
if (a[j].title == t && a[j].artist == ar)
{
cout << "The id of " << t << " by " << ar << " is " << a[j].id << endl;
flag = 1;
}
}
if (flag == 0)
cout << "Artist and title not found!" << endl;
break;
case 5:
cout << "Quitting!" << endl;
break;
default:
cout << "Wrong Input!" << endl;
}
}
system("pause");
return 0;
}
|