Encode a string using Caesar's Cipher My program is complete and running, but I would like second opinions on how I can make it more efficient. The program prompts the user for the message and the amount s/he would like to shift the letters. For the implementation part, is there another "faster" way to do it instead of having the if else if statements to run through and test my characters? 

#include "stdafx.h"
#include &lt;iostream&gt; 
#include &lt;string&gt; 

using namespace std; 

string encodeCaesarCipher(string str, int shift); 

int main()
{
    string str, encoded_string;  
    int shift; 

    cout &lt;&lt; "This program encodes a message in Caesar Cipher. \n\n"; 

    cout &lt;&lt; "Please enter a message: "; 
    getline(cin, str);  

    cout &lt;&lt; "\nHow many times do you want to shift the letter(s) in the message? ";
    cin &gt;&gt; shift; 

    while (shift &lt; 1)
    {
        cout &lt;&lt; "You have entered an invalid number. \n"; 
        cout &lt;&lt; "Please try again. \n"; 
        cout &lt;&lt; "Enter a proper number for the amount of letter shifting: "; 
        cin &gt;&gt; shift; 
    }

    encoded_string = encodeCaesarCipher(str, shift); 

    cout &lt;&lt; "\nEncoded message: " &lt;&lt; encoded_string &lt;&lt; "\n\n"; 

    system("PAUSE"); 

    return 0;
}

string encodeCaesarCipher(string str, int shift)
{
    string temp = str; 
    int length; 

    length = (int)temp.length(); 

    for (int i = 0; i &lt; length; i++)
    {
        if (isalpha(temp[i]))
        {
            for (int j = 0; j &lt; shift; j++)
            {
                if (temp[i] == 'z')
                {
                    temp[i] = 'a'; 
                }
                else if (temp[i] == 'Z')
                {
                    temp[i] = 'A'; 
                }
                else 
                {
                    temp[i]++; 
                }
            }
        }
    }

    return temp; 
}
Optimization difficulty  is about quadratic on the length of a function. I suggest a separate shift function, like

 char shift(char ch, int shift)
 {
            if (ch == 'z')
            {
                 return 'a'; 
            }
            else if (ch== 'Z')
            {
                return 'A'; 
            }
            else 
            {
                return ch + 1;  
            }
   }


And you can call it in the loop.

Anyhow this programme does not need speed up, runtime is dominated by input and output taking.

Avoid namespace std;, return 0, and system("pause").
The first causes name clashes, the second is not needed, the third is a portability issue and a security breach.

As a minor note, declare and assign length on the same line:

  int length = (int) str.length()


Following @Ryan 's suggestion, -25 can be used to merge 'z' and 'Z'. It then becomes so short to beg for a ternary:

 char shift(char ch, int shift)
 {
     return ch == 'z' || ch == 'Z' ? ch - 25 : ch + 1;
 }