I wanted to sort the inputs by ascending order. but it seems to be sorting the integers only.. I based the sorting method on Bubble Sort.. Sorry if my post is too messy.. My first post here :(
using namespace std;
struct movies_t{
string title;
int year;
}films[3];
void printmovie (movies_t movie);
int main ()
{
string mystr;
int n,t;
string d;
for(n=0;n<3;n++)
{
cout <<"Enter title:";
getline(cin,films[n].title);
cout <<"Enter year:";
getline(cin,mystr);
stringstream (mystr)>> films[n].year;
}
for(n=0;n<3;n++)
{
for(int y=0; y<3; y++)
{
if(films[n].year < films[y].year)
{
t = films[n].year;
films[n].year = films[y].year;
films[y].year = t;
}
if(films[n].title < films[y].title)
{
d = films[n].title;
films[n].title = films[y].title;
films[y].title = d;
}
}
}
cout <<"\n You have entered these movies:\n";
for(n=0;n<3;n++)
printmovie (films[n]);
return (0);
}
void printmovie(movies_t movie)
{
cout <<movie.title;
cout <<"("<<movie.year<<")\n";
}
The Ouput
Enter title:a
Enter year:2001
Enter title:d
Enter year:2011
Enter title:f
Enter year:2005
You have entered these movies:
a(2001)
d(2005)
f(2011)
I wanted the output to be:
You have entered these movies:
a(2001)
f(2005)
d(2011)