sorting the title

Apr 26, 2013 at 5:37pm
/*
*VsPod.cpp
*4-23-2013
*/

#include<iostream>
#include<vector>
#include<string>

using namespace std;

struct song
{
string t;
string a;
double l;
};

//A is less than B
void Add(vector<song> &l)
{
song ns;
cout<<"What is the name of the song?"<<endl;
getline(cin,ns.t);
cout<<"What is the artist name?"<<endl;
getline(cin,ns.a);
cout<<"What is the length of the song?"<<endl;
cin>>ns.l;
l.push_back(ns);
}
void Title(song.t)
{
string tt;

if(t>tt)
{

}

}
void Playlist()
{

}
int main()
{
vector<song>l;
int x;
while(x!=6)
{
cout<<"Hello welcome to VSPod."<<endl;
cout<<"(1) Add a song."<<endl;
cout<<"(2) Sort by title."<<endl;
cout<<"(3) Sort by artist."<<endl;
cout<<"(4) Sort by length."<<endl;
cout<<"(5) Print your playlist."<<endl;
cout<<"(6) EXIT."<<endl;
cout<<"Pick one:";
cin>>x;
cin.clear();
cin.sync();
if(x==1)
{
Add(l);
}
if(x==2)
{
Title();
}
if(x==3)
{

}
if(x==4)
{

}
if(x==5)
{

}
}
}

this is my code and my problem is im trying to sort the title but i cant acess the title file
Last edited on Apr 28, 2013 at 9:59pm
Apr 26, 2013 at 6:11pm
You do have a std::vector<song>. See std::sort
http://www.cplusplus.com/reference/algorithm/sort/

Define a functor that takes two songs, compares their member t and returns result. t is std::string, so it already has operator<
Apr 28, 2013 at 5:43pm
my problem wasn't how to compare the two songs but what i called the second song when i compared because i dont understand how to acess another song title because in the example from your link they have to variables that they compare to


Last edited on Apr 28, 2013 at 5:44pm
Apr 28, 2013 at 7:16pm
At some point in your code you do use "ns.t", where "ns" is of type "song", and that type has member "t". So, what does that piece of code do?
Apr 28, 2013 at 9:16pm
t stands for the title and ns is where the song the user enters is stored and each time the user enters a song it gets stored in l...do you know who to access the different ns in the vector l?
Apr 29, 2013 at 6:32am
http://www.cplusplus.com/reference/vector/vector/

Multiple ways to access an element: operator[], at(), iterators. For example, first title (if l is not empty)
std::string title = l[0].t;
Apr 29, 2013 at 10:37am
Okay. That makes sense. I thought that since it wasn't a number being stored I couldn't just put o in the brackets. do you think that this sorting of the title is correct:

void Title(vector<song>l)
{
int n;
n++;
string t;
string tt;
for (int i=0; i<n; i++)
{
if(l[i].t>='a')
{
string t=l[i].t;
string tt=l[i+1].t;
if(t<tt)
{
cout<<l[i].t<<endl;
cout<<l[i+1].t<<endl;
}
}
}

:I'm trying to print to the other the songs in order from A->Z using the title
Topic archived. No new replies allowed.