By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

I want to re-arrange elements depending on input

P: n/a
I am creating a program that should allow user to enter Top 20 hit songs.
The user should enter song name and artist name in order of position.The output should be printed in table format and should be allow user to re-arrange songs later if she want to do so.
The user has to enter song name and the new position she wants the song moved to (this means she chooses from the 20 filled positions).For example if the user want to take a song from position10 to position1, the song will make the song in position1 move one position down and the same to the rest of the song

So the problem I have is how to make this work, i have a little knowldge in sorting arrays of which I think could help. Can Someone help me copmlete the following code:

Expand|Select|Wrap|Line Numbers
  1. //last part of the code
  2. //print data
  3.  for int pos=0;pos<20;pos++
  4. cout<<setw(5)<<(pos+1)<<setw(15)<<song[pos]<<setw(15)<<artist[pos]<<endl;
  6. //here i need to tell the program to move songs one step up or down depending on user choice
Oct 29 '10 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 9,197
I see you are using cout so that means you are using C++.

That being the case, I recommend you use the C++ Standard Library list<> template for your database. Use C++ string objects for your data fields.

All of the moving around you need to do is already coded in this list<> template, which implements a double-linked list.

To make things easy, declare a struct to contain your data items and make a lst of variables of that struct.

When you sort, you can write a function that compares two of your struct variables and returns a true if the first argument is greater than the second aregument and false otherwise. Then you can use the C++ Standard Library sort<> template. You use iterators to the start and end of the list plus the address of your compare function. Voila! your list is sorted.

Almost all C++ textbooks have examples of how to do exactly this.
Oct 29 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.