944,141 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 699
  • C++ RSS
Oct 10th, 2007
0

String sorting troubles

Expand Post »
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?
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
HoldenCfld is offline Offline
6 posts
since Sep 2007
Oct 10th, 2007
0

Re: String sorting troubles

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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 10th, 2007
0

Re: String sorting troubles

x
Last edited by HoldenCfld; Oct 10th, 2007 at 2:50 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
HoldenCfld is offline Offline
6 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Explicit template instantiation
Next Thread in C++ Forum Timeline: Converting a String to an int array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC