Strings in Alphabetical Order

Hello. I am trying to create a program which will sort strings, input by the user, into alphabetical order. So for example, if the user inputs Henry, Derik, Steven, in that order, the output should be Derik, Henry, Steven. This is the code I have so far. Any help is appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "std_lib_facilities.h"

int main()
{
    string val1, val2, val3;

    cout << "Please enter three string values:\n";
    cin >> val1;
    cin >> val2;
    cin >> val3;

    if (val2 > val3)
    {
        cout << val1 << "," << val3 << "," << val2;
    }

    else if (val1 > val2)
    {
        cout << val2 << "," << val1 << "," << val3;
    }

    else
    {
        cout << val1 << "," << val2 << "," << val3;
    }

    return 0;
}
Last edited on
You need to place them in a collection. The code just becomes impossible as you start adding more strings.

A strings knows about order, that's why you can compare them. So, you just need to find a standard container that holds things in order, and use that.
Thanks kbw. Unfortunately, I'm relatively new to programming, so what you said makes very little sense to me. I don't know what a collection or a standard container is. I'm trying to get this to work as it is an exercise in my book, but I have been struggling with it for over two hours now. If it's not too much trouble, could you show me an example of how to create a basic sorting algorithm for strings?
Click on Reference on the left menu, then Containers. Things like std::vector and std::deque are most likely what you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
  vector <string> names;

  cout << "Please enter a bunch of names, one per line.\n"
          "Press Enter twice to finish\n> ";
  string name;
  while (getline( cin, name ) && !s.empty())
  {
    names.push_back( name );
    cout << "> ";
  }

  sort( names.begin(), names.end() );

  cout << "You entered " << names.size() << " names. They are:\n";
  for (auto name : names)
  {
    cout << name << "\n";
  }
}

Hope this helps.
There's a list of standard containers here: http://en.cppreference.com/w/cpp/container

Some are sorted, some don't allow duplicates, some hold key/value pairs, ... there's quite a variety.
@Duoas he have not learned vector yet
this is my code you should make another 3 type string which will sort the values
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "../../aw.h"

int main()
{
    string val1, val2, val3, first, second,third;

    cout << "Please enter three string values:\n";
    cin >> val1;
    cin >> val2;
    cin >> val3;

if(val1<=val2 && val1<=val3)
{
    first=val1;
    if(val2<=val3)
    {
        second=val2;
        third=val3;
    }
    else
    {
        second=val3;
        third=val2;
    }
}
else if(val2<=val3 && val2<=val3)
{
    if(val1<=val3)
    {
        first =val1;
        second <=val3;
    }
    else
    {
        first<=val3;
        second<=val1;
    }
}
else
{
    first= val3;
    if(val1<=val2)
    {
        second=val1;
        third=val2;
    }
    else
    {
        second=val2;
        third=val1;
    }

}
cout<<first<<" , "<<second<< " , "<< third; //value sort
}
Last edited on
If you are sorting three things, there's a trick.

Put the first two items in order.
Then the second and third. (At this point third item will be in its final place.)
Then the first two again. (Putting them in their final place.)
Thanks for the replies.

@Duoas:
Thanks for the directions, but that stuff makes no sense to me yet. I'm really trying to keep this as simple as possible. As for the trick, I'm probably doing it wrong, but it's still not working. Perhaps you could illustrate with some code?

@justinelandichoruiz:
Thanks for sharing your code. I ran some tests, using the letters 'a', 'b' and 'c' for control. These are the results I got back:

abc...a,b,c
acb...a,b,c
bac...b, ,
bca...a,b,c
cab... , ,
cba...a,b,c

As far as I can see every input works correctly except for when:
 
val1 > val2 && val1 < val3

and
 
val1 > val2 && val1 > val3

I find it strange that a respective input of b, a and c results in two empty output spaces, and that a respective input of c, a and b results in three empty output spaces. Any ideas?
@Bogeyman oh sorry look at my code in line 31,line 35, line 36 change them into = sign
edited: wait not just 31,35,36 im going to edit it wait
Last edited on
fixed code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "../../aw.h"

int main()
{
    string val1, val2, val3, first, second,third;

    cout << "Please enter three string values:\n";
    cin >> val1;
    cin >> val2;
    cin >> val3;

if(val1<=val2 && val1<=val3)
{
    first=val1;
    if(val2<=val3)
    {
        second=val2;
        third=val3;
    }
    else
    {
        second=val3;
        third=val2;
    }
}
else if(val2<=val3 && val2<=val3)
{
    first = val2;
    if(val1<=val3)
    {
        second = val1;
       third =val3;
    }
    else
    {
        second=val3;
        third=val1;
    }
}
else
{
    first= val3;
    if(val1<=val2)
    {
        second=val1;
        third=val2;
    }
    else
    {
        second=val2;
        third=val1;
    }

}
cout<<first<<" , "<<second<< " , "<< third; //value sort
}
Sorry for the late reply. Thank you, it works now.
Topic archived. No new replies allowed.