C++ Pig Latin program
See at @Caridorc first.

Use the full power of the stream.

The standard stream operators will read a word at a time for you.

std::string word;
while(std::cin &gt;&gt; word)
{
     // DO STUFF.
}


The above will read a file. If you just want to read a line. Read a line then use string stream.

// Read a line
std::string line;
std::getline(std::cin, line);

// convert line to stream
std::stringstream linestream(line);

// Loop over it one word at a time.
std::string word;
while(linestream &gt;&gt; word)
{
     // Operate on word.
}


DRY your code

Both sides of your if statement are doing the same thing. The difference is handling the last word (one not terminated by a space). You should move common code outside the if statement just leave the non common code in the if statement.

    if (&lt;test&gt;)
    {
    }
    else
    {
    }

    // This all seems common across the two if statements.
    pigLatWord = word.substr(index);
    length = pigLatWord.length();
    pigLatWord.insert(length, "ay");
    pigLatWord.insert(length, 1, word[index]);
    pigLatWord.erase(0, 1);
    index = word.length();


Declaration when needed.

Put your variable declarations as close to the point of use as you can. This way you don't need to look for their type very far (it is where the code is). They only get constructed when you need them (and destroyed as soon as possible) so preventing excessive use of space and not costing anything if you don't need them.

// These could be moved to the other side of the if.
// As pigWord is only used after the loop (in my new version)
string pigLatWord;


Declare one variable per line.

string pigLatWord, pigLatSentence = "";
int length = 0, index = 0;


Humans need help in reading code. Don't make it hard for them. Declare one variable per line. This also helps when refactoring code later. You will only move the variables you need and source control tools will find it easier to do a diff on the change.

Pass by const reference.

If you do not plan on modifying the input pass by const reference to prevent a copy.

string pigLatin(string const&amp; word){
             //        ^^^^^^


Design.

If I was actually going to write this I would encapsulate the concept of a Pig Latin word in a class. Then you can localize the change in a single well named class. You can then make use of standard routines to help read/print.

class PigLatinWord
{
    // STUFF
};

int main()
{
    // Copies std::cin to std::cout converting to PigLatin
    // on the copy to the output stream.
    std::copy(std::istream_iterator&lt;std::string&gt;(std::cin),
              std::istream_iterator&lt;std::string&gt;(),
              std::ostream_iterator&lt;PigLatinWord&gt;(std::cout, " ")
             );
}