Sorting words alphabetically using if/else statements

Hello guys. I am just a few weeks into my C++ course. I am working on an assignment in which the user has to input three names into the console and then they have to be arranged in alphabetical order. I can only use if/else statements to do so (nothing more advanced). I cannot seem to figure it out and I have searched all over the web for an answer to no avail. How would I solve this? This is the code I have so far.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
string name1, name2, name3, displayName1, displayName2, displayName3;

// Reads 3 names.
cout << "A name is a single word with no spaces.\n";
cout << "Enter 3 DIFFERENT names separated by spaces:\n";
cin >> name1 >> name2 >> name3;
cout << endl;

if ((name1 < name2) && (name1 < name3))
{
else if (name2 < name3)
{
displayName1 = name1;
displayName2 = name2;
displayName3 = name3;
}
else if(name2 > name3)
{
displayName1 = name1;
displayName2 = name3;
displayName3 = name2;
}
else
cout << "Error." << endl;
}

if ((name2 < name1) && (name2 < name3))
{
if (name1 < name3)
{
displayName1 = name2;
displayName2 = name1;
displayName3 = name3;
}
else if(name1 > name3)
{
displayName1 = name2;
displayName2 = name3;
displayName3 = name1;
}
else
cout << "Error." << endl;
}
if ((name3 < name1) && (name3 < name2))
{
if (name1 < name2)
{
displayName1 = name3;
displayName2 = name1;
displayName3 = name2;
}
else if(name1 > name2)
{
displayName1 = name3;
displayName2 = name2;
displayName3 = name1;
}
else
cout << "Error." << endl;
}



// Display.
cout << "1234567890123456789012345678901234567890";
cout << "1234567890123456789012345678901234567890\n";
cout << setw(20) << left << displayName1 << setw(20) << left << displayName2 << setw(20) << left << displayName3 << endl;

cout << endl;
system("pause");
return 0;
}
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
string name1, name2, name3, displayName1, displayName2, displayName3;

// Reads 3 names.
cout << "A name is a single word with no spaces.\n";
cout << "Enter 3 DIFFERENT names separated by spaces:\n";
cin >> name1 >> name2 >> name3;
cout << endl;

if ((name1 < name2) && (name1 < name3))
{
else if (name2 < name3)
{
displayName1 = name1;
displayName2 = name2;
displayName3 = name3;
}
else if(name2 > name3)
{
displayName1 = name1;
displayName2 = name3;
displayName3 = name2;
}
else
cout << "Error." << endl;
}

if ((name2 < name1) && (name2 < name3))
{
if (name1 < name3)
{
displayName1 = name2;
displayName2 = name1;
displayName3 = name3;
}
else if(name1 > name3)
{
displayName1 = name2;
displayName2 = name3;
displayName3 = name1;
}
else
cout << "Error." << endl;
}
if ((name3 < name1) && (name3 < name2))
{
if (name1 < name2)
{
displayName1 = name3;
displayName2 = name1;
displayName3 = name2;
}
else if(name1 > name2)
{
displayName1 = name3;
displayName2 = name2;
displayName3 = name1;
}
else
cout << "Error." << endl;
}



// Display.
cout << "1234567890123456789012345678901234567890";
cout << "1234567890123456789012345678901234567890\n";
cout << setw(20) << left << displayName1 << setw(20) << left << displayName2 << setw(20) << left << displayName3 << endl;

cout << endl;
system("pause");
return 0;
}


replace the else if on line 18 with if. The first else if statement in a chain of if-else if statements has to be preceded by an if statement. Also, you can change the if on line 34 to an else if and replace all of line 51 with else.
Last edited on
Thank you. I followed your advice except with replacing line 51 with else. When I did that I got a compiler error saying "; expected before {", so I changed line 51 back to else if and it worked.
When I compile it, it works. I'm thinking you only replaced the if with else. What I meant to say was replace if ((name3 < name1) && (name3 < name2)) with else.
Topic archived. No new replies allowed.