954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Sorting 3 strings in c++ using if else-if

I need to write a program to enter 3 names then display the name is aphabetical order. Example Jessie, sam, and chris. the program should display:
chris
Jessie
sam

Please help

JuliaCplus
Newbie Poster
2 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

>>I need to write a program

Ok, so write it :)

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

If I could write it, I wouldn't be asking for help. i can sort 2 strings but can't sort 3 strings.
// Get the first name.
cout << "Enter a name (last name first): ";
cin.getline(name1, SIZE);

// Get the second name.
cout << "Enter another name: ";
cin.getline(name2, SIZE);

// Display them sorted in alphabetical order.
cout << " Here are the names sorted alphabetically:\n";
if (strcmp(name1, name2) < 0)
cout << name1 << endl << name2 << endl;
else if (strcmp(name1, name2) > 0)
cout << name2 << endl << name1 << endl;
else
cout << "You entered the same name twice!\n";
return 0;

JuliaCplus
Newbie Poster
2 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

>>If I could write it, I wouldn't be asking for help.
This doesn't work this way. Ancient Dragon wanted you to tell that you need to give more detail and the account of the effort you put in.
Also, everyone is supposed to read the Sticky thread: http://www.daniweb.com/forums/thread78223.html and also the Announcement: http://www.daniweb.com/forums/announcement8-2.html

Moreover, you are suppose to put your code in the code tags.

Now as far as your problem is concerned:
You need to get some lesson on sorting. Read the following article I found by using Google: http://www.daniweb.com/forums/thread6713.html

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

>i can sort 2 strings but can't sort 3 strings.
If you can sort two strings, you know how to sort three. This is especially easy because there's a smallest, a largest, and only one inbetween. So all you need to do is find the smallest and largest before you can determine the order.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Sorting any type of data with use of the STL:

http://www.cplusplus.com/reference/algorithm/sort/

Which may be a bit too much for 3 strings, but you'll know how to sort 2000 too! :D

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You