• Post Reply Bookmark Topic Watch Topic
  • New Topic

String sort alphabetically  RSS feed

 
Daniel T. Williams
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I'm trying to sort 3 strings entered by a user in alphabetic order.
It works fine until the last else if statement and will only print 2 of the strings.
This is a project for school and the instructor wants us to use If/else statements right now as we haven't gotten to arrays.
Any help on why the last statement only prints 2 strings would be appreciated. Thank you.
/**



 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel T. Williams wrote:Hello. I'm trying to sort 3 strings entered by a user in alphabetic order.
It works fine until the last else if statement and will only print 2 of the strings.
This is a project for school and the instructor wants us to use If/else statements right now as we haven't gotten to arrays.
Any help on why the last statement only prints 2 strings would be appreciated. Thank you.


Well, simply, you didn't take care of all possible cases. And it is not just the last case, it can happen with the earlier cases too. Take the first block of the if-then-else chain...



If sentence1 is less than sentence2, but is *not* less than sentence3, then only two lines will be printed.... and those two lines will be either be sentence2 followed by sentence3 or sentence3 followed by sentence2.

Henry
 
Daniel T. Williams
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it was something simple that I wasn't seeing. Sometimes it is helpful to have another set of eyes look at it. Thank you.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you been told to use that technique for sorting objects? It looks very cumbersome, particularly when there are many sorting algorithms available. Also, have you really been taught to use such a long main() method?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Have you been told to use that technique for sorting objects? It looks very cumbersome, particularly when there are many sorting algorithms available.

@Daniel: And what happens if next time you have 4 Strings to sort? Or 5? Or 500? Do you propose to write the entire process out in longhand then?

If this is simply an exercise in logic (and if so, it's not a bad one), then one thing that might help is to write out a truth table. That at least should help you to cover all bases.

HIH

Winston
 
Daniel T. Williams
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Have you been told to use that technique for sorting objects? It looks very cumbersome, particularly when there are many sorting algorithms available. Also, have you really been taught to use such a long main() method?


Thank you for the welcome

The main method is part of a template that the instructor wants us to use on each project we do.

Campbell Ritchie wrote:@Daniel: And what happens if next time you have 4 Strings to sort? Or 5? Or 500? Do you propose to write the entire process out in longhand then?

If this is simply an exercise in logic (and if so, it's not a bad one), then one thing that might help is to write out a truth table. That at least should help you to cover all bases.

I'm pretty sure this assignment was just an excerise in logic as you suggested. In fact I did end up using a truth table later to figure this out. In the upcoming weeks we will be doing switch statements and then arrays, so these things should be easier. Thanks for the help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!