Sorting string with if-else I was asked to write a program that sorts three strings alphabetically using only if-else (no sorting algorithms, usage of arrays whatsover). I'm fairly new to C++ programming but I've come up with this program and would like to know if there's any way to make this more efficient while only using if-else? Any help or suggestions would be appreciated. Thank you!
#include &lt;iostream&gt;
using namespace std;

int main()
{
    string word1, word2, word3;
    string temp;
    
    cout &lt;&lt; &quot;Enter three words separated by a space: &quot;;
    cin &gt;&gt; word1 &gt;&gt; word2 &gt;&gt; word3; 
    
    if (word1 &gt; word2 &amp;&amp; word1 &gt; word3 &amp;&amp; word3 &gt; word2) 
    {
        temp = word1; 
        word1 = word2; 
        word2 = word3; 
        word3 = temp; 
    } 
        
        else if (word1 &gt; word2 &amp;&amp; word1 &gt; word3 &amp;&amp; word2 &gt; word3) 
        {
            temp = word1; 
            word1 = word3;
            word3 = temp; 
        }
        
            else if (word2 &gt; word1 &amp;&amp; word2 &gt; word3 &amp;&amp; word1 &gt; word3)
            {
                temp = word2; 
                word1 = word3; 
                word2 = word1; 
                word3 = temp;
            }
            
            else if (word2 &gt; word1 &amp;&amp; word2 &gt; word3 &amp;&amp; word3 &gt; word1)
            {
                temp = word2; 
                word2 = word3; 
                word3 = temp; 
            }
            
                else if (word3 &gt; word1 &amp;&amp; word3 &gt; word2 &amp;&amp; word1 &gt; word2)
                {
                    temp = word1; 
                    word1 = word2; 
                    word2 = temp; 
                }

    
    cout &lt;&lt; &quot;The correct sort is &quot; &lt;&lt; word1 &lt;&lt; &quot;, &quot; &lt;&lt; word2 &lt;&lt; &quot;, &quot; &lt;&lt; word3 &lt;&lt; endl;
    
    return 0;
}
Your indentation is off. if should be at the same level as the corresponding else or else if, their bodies being indented once. Don't wander all the way to the right.

You are missing the include for std::string, &lt;string&gt;.

using namespace std; seems convenient, right? Unfortunately, throwing everything and the kitchen sink into the global namespace and praying it works is error-prone.
See &quot;Why is “using namespace std;” considered bad practice?&quot;.
I wouldn't even use a using-declaration here, though that would be unproblematic at least.

return 0; is implicit for main(). (And only for that function.)

You never test whether reading (and/or writing) succeeds. At least that's pretty harmless in your program. Still, consider ending with:
    return !!std::cin || !!std::cout;


Swapping is often far more efficient than copying. Consider using a.swap(b) or the more generic using std::swap; swap(a, b); two-step, the latter is in &lt;utility&gt;.

Only use std::endl when you need to flush the stream. Actually, belay that, be explicit and use std::flush.
Hint: std::cin and std::cout are tied, which you already take advantage of with your prompt. Also, when the program ends the standard streams are flushed.
See &quot;What is the C++ iostream endl fiasco?&quot;.

Comparing strings is likely to be comparatively expensive. Thus, compare two strings, and then make different decisions depending on the outcome.

An alternative to considering each case separately is writing a sorting-network.
You would be well-advised to abstract a conditional swap into its own function if you go that way:
void sort2(std::string&amp; a, std::string&amp; b) noexcept {
    if (b &lt; a)
        b.swap(a);
}