String sorting troubles

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 6
Reputation: HoldenCfld is an unknown quantity at this point 
Solved Threads: 0
HoldenCfld HoldenCfld is offline Offline
Newbie Poster

String sorting troubles

 
0
  #1
Oct 10th, 2007
I have to take in a list of song names. When the user types in "mmm" then it stops, and it spits out the first and last song titles in a sorted list (alphabetically). This is what I came up with, and there's many an error, all seemingly bool errors.

So, any suggestions?
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string songTitles;
  9. int songMax = 0;
  10. int songMin = 0;
  11.  
  12. while (songTitles != "mmm")
  13. {
  14. cout << "Please enter a song title: ";
  15. getline (cin, "songTitle")
  16.  
  17. if (songTitles != "mmm")
  18. songTitles = songMin;
  19.  
  20. if (songTitles < songMin && songTitles != "mmm")
  21. songTitles = songMin;
  22.  
  23. if (songTitles > songMin && songTitles != "mmm")
  24. songTitles = songMax;
  25.  
  26. if (songTitles > songMax && songTitles != "mmm")
  27. songTitles = songMax;
  28.  
  29. }
  30.  
  31. cout << "First song: " << songMin;
  32. cout << "Last song: " << songMax;
  33.  
  34.  
  35. return 0;
  36.  
  37. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: String sorting troubles

 
0
  #2
Oct 10th, 2007
How are the integers songMin and songMax going to hold the titles of the respective first and last titles?

Your branching code should be done as a series of if...else if statements - when any one of the three (not four) possible conditions is met, there's no point in continuing testing.

Look at the order of the assignments you make. Remember that Left hand side is assigned what's on the Right hand side. ( LHS <- RHS )

See my comments on min/max testing in an earlier thread.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 6
Reputation: HoldenCfld is an unknown quantity at this point 
Solved Threads: 0
HoldenCfld HoldenCfld is offline Offline
Newbie Poster

Re: String sorting troubles

 
0
  #3
Oct 10th, 2007
x
Last edited by HoldenCfld; Oct 10th, 2007 at 2:50 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 617 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC