Hey all,

I'm a beginner programmer and need help here. I can't figure out how to translate a word that begins with a consonant. I have all the code to translate a word that begins with a vowel, and those words translate correctly, but I cannot get the words with consonants to translate correctly. I have seen lots of examples on here of Pig Latin programs, but they don't seem to follow the type of parameters that we have to use for this class. Any help would really be appreciated, as I have been working like crazy trying to get this thing right, and I simply cannot.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string getMenuOption();
string translateLine(string);
string translateWord(string);
string getNextWord(string, int);
bool isVowel(char);
bool isPuncutation(char);
void splitWord(string, int, string&, string&);
//void splitWord(string, int, string&, string&);

// Main
int main()
        string sentence, pigSentence;
        string menuChoice;

        menuChoice = getMenuOption();
{
        string sentence, pigSentence;
        string menuChoice;

        menuChoice = getMenuOption();
    if (menuChoice == "1")
    {
        ifstream inputFile;
        inputFile.open("Program5Input.txt");
        inputFile >> sentence;

    }
    else
    {
        cout << "Your sentence: ";
        cout << endl;
        getline(cin, sentence);
        // call a function to translate a sentence
        pigSentence = translateLine(sentence);
        cout << pigSentence << endl;
    }

    system("pause");
    return 0;
}


// Menu
string getMenuOption()
{
    string menuChoice;


    cout << "[1] to process a file";
    cout << endl;
    cout << "[2] to process a single sentence";
    cout << endl;
    cout << "Your choice: ";
    getline(cin, menuChoice);

    return menuChoice;
}

// Translate Line Function
string translateLine(string sentence)
{
    int i = 0, length;
    length = sentence.length();
    string word, pigWord, pigSentence="";
    while (i < length)
    {
        word = getNextWord(sentence, i);
        i = i + word.length() + 1;
        pigWord = translateWord(word);
        pigSentence = pigSentence + pigWord + " ";
    }

    return pigSentence;
}

// Get Next Word Function
string getNextWord(string sentence, int i)
{
    string word;
    char letter;
    letter = sentence[i];
    while (letter != ' ' && i < sentence.length())
    {
        word = word + letter;
        i = i + 1;
        if (i < sentence.length())
            letter = sentence[i];
    }
    return word;
}

// Translate Word Function
string translateWord(string word)
{
    int i = 0;
    string pigWord;
    string first = "";
    string second = "";
    if (isVowel(word[0]) == true)
    {
        pigWord = word + "way";
    }
    else 
    {
        // THIS IS WHAT I CAN'T FIGURE OUT
        while (isVowel(word[i]) == false)
        {
            first = first + word[i];
            i++;
        }

        splitWord(word, i, first, second);
        pigWord = first + "ay";
    }

    return pigWord;
}

// Determine Whether Character is a Vowel
bool isVowel(char letter)
{
    bool vowel;
    letter = tolower(letter);

    if (letter == 'a' || letter == 'e' || 
        letter == 'i' || letter == 'o' || 
        letter == 'u' )
    {
        vowel = true;
    }
    else 
    {
        vowel = false;
    }
    return vowel;
}

void splitWord(string word, int i, string& first, string& second)
{
    int l;
    l = 0;
    while (l < i)
    {
        first = first + word[i];
        l++;
    }
    while (i < word.length())
    {

    second = second + word[i];

but I cannot get the words with consonants to translate correctly. I have seen lots of examples on here of Pig Latin programs, but they don't seem to follow the type of parameters that we have to use for this class. Any help would really be appreciated, as I have been working like crazy trying to get this thing right, and I simply cannot.

How are your parameters different from the standard Pig Latin? If they are so different, it really isn't Pig Latin. Isn't it just move the consonants to the end of the word and at "ay"? That's how all Pig Latin programs work.

You might want do describe what you are supposed to do and what is really happening so we have something we can understand.

I'm sorry, the Pig Latin is the same as what I am seeing on the other pages, but how it is being written is confusing me. This is a beginners C++ course and I"m having a hard time following what the previous posters have without the given functions I have within my program.

The rules we were given are the following:

Words that start with a vowel (A, E, I, O, U) simply have "way" appended to the end of the word.
Words that start with a consonant have all consonant letters up to the first vowel moved to the end of the word, and then "AY" is appended.
Ensure proper capitalization is kept.
Ensure punctuation is preserved (periods and commas should appear after translated words in the same places.)
Correctly translate qu (example: ietquay instead of uietqay).
Differentiate between y as a vowel and y as a consonant.

Right now, I can translate a word that begins with a vowel correctly, but I am unsure how to write code that translates a word that begins with consonants into Pig Latin. I'm sorry if this sounds stupid, I have spent an upwards of 12 hours on this over the last two days and can simply not figure out how to write code to translate consonants.

Thanks in advance for any help given.

I have figured out how to get the program to translate consonants, but I am still stuck on two steps.

  1. Writing something to differentiate whether y is being used a vowel (ie: my) or as a consonant (ie: yellow) and keeping qu together. (quack -> ackqu)
  2. Keep punctuation in the correct places after the word has been translated. (ie: this, i say -> isth, iway aysay)

Any kept would be greatly appreciated.

Here is my updated code.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string getMenuOption();
string translateLine(string);
string translateWord(string);
string getNextWord(string, int);
bool isVowel(char);
bool isPuncutation(char);

// Main
int main()
{
    string sentence, pigSentence;
    string menuChoice;

    menuChoice = getMenuOption();
    if (menuChoice == "1")
    {
        ifstream inputFile;
        ofstream outputFile;
        inputFile.open("Program5Input.txt");
        outputFile.open("Program5Output.txt");
        while (inputFile >> sentence)
        {
        pigSentence = translateLine(sentence);
        outputFile << pigSentence;
        }

    }
    else
    {
        cout << "Your sentence: ";
        cout << endl;
        getline(cin, sentence);
        // call a function to translate a sentence
        pigSentence = translateLine(sentence);
        cout << pigSentence << endl;
    }

    system("pause");
    return 0;
}


// Menu
string getMenuOption()
{
    string menuChoice;


    cout << "[1] to process a file";
    cout << endl;
    cout << "[2] to process a single sentence";
    cout << endl;
    cout << "Your choice: ";
    getline(cin, menuChoice);

    return menuChoice;
}

// Translate Line Function
string translateLine(string sentence)
{
    int i = 0, length;
    length = sentence.length();
    string word, pigWord, pigSentence="";
    while (i < length)
    {
        word = getNextWord(sentence, i);
        i = i + word.length() + 1;
        pigWord = translateWord(word);
        pigSentence = pigSentence + pigWord + " ";
    }

    return pigSentence;
}

// Get Next Word Function
string getNextWord(string sentence, int i)
{
    string word;
    char letter;
    letter = sentence[i];
    while (letter != ' ' && i < sentence.length())
    {
        word = word + letter;
        i = i + 1;
        if (i < sentence.length())
            letter = sentence[i];
    }
    return word;
}

// Translate Word Function
string translateWord(string word)
{
    int i = 0;
    string pigWord;
    string first = "";
    string second = "";
    if (isVowel(word[0]) == true)
    {
        pigWord = word + "way";
    }
    else
    {
        if (isVowel(word[i]) == false)
        {
            while (i < word.length() && word[i] != 'a' && word[i] != 'e' && word[i] != 'i' && word[i] != 'o' && word[i] != 'u')
            {
                first = first + word[i];
                i++;
            }

            while (i < word.length())
            {
                second = second + word[i];
                i++;
            }

        }
        pigWord = second + first + "ay";
    }
    return pigWord;
}

// Determine Whether Character is a Vowel
bool isVowel(char letter)
{
    bool vowel;
    letter = tolower(letter);

    if (letter == 'a' || letter == 'e' || 
        letter == 'i' || letter == 'o' || 
        letter == 'u' )
    {
        vowel = true;
    }
    else 
    {
        vowel = false;
    }
    return vowel;
}

I started to look at your issues but wound up rewritting the whole translateWord. Not what I wanted. So, I don't know if this will help, I will post the pseudocode. That way you can see some of the things I did to handle you issues.
Differentiate between y as a vowel and y as a consonant is still an issue. Need for information about the rule I should use to deteremine that.

I used the member functions of C++ strings to make it easier. Since you are using strings, then use all of the power that comes with them.
http://www.cplusplus.com/reference/string/string/find_first_of
http://www.cplusplus.com/reference/string/string/assign
http://www.cplusplus.com/reference/string/string/erase
http://www.cplusplus.com/reference/string/string/substr
http://www.cplusplus.com/reference/string/string/npos

string translateWord(string word){
    string pigWord;
    //  If the first character upper case, set variable to use later
    bool   firstCharIsUpper(isupper(word[0]));
    // Find first the vowel
    size_t vowelPos(word.find_first_of("aeiouAEIOU"));

   // Need to handle qu, by testing for it and if found, find the next vowel
   // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   // Find the punctuation ".?:;!,"
   // Warning:  Not handling more than one punctuation in word
   // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   size_t punctPos(word.find_first_of(".?:;!,"));
   // If the first letter is a vowel
       // If there is punctuation
          // Save off the punctuation
          // Remove punctuation
       // Create new word using word, "way" and maybe pucntuation
   // Else the first letter is not a vowel
       // If first was upper, make first letter lower
       // If there is a vowel
          // If there was not punctuation
            // Create new word using vowel position and "ay"
          // Else there was punctuation
            // Save the punctuation
            // Remove punctuation from word
            // Create new word using vowel position and "ay" and pucntuation
          // END if there was not punctuation
       // Else there was no vowel found 
          //+++++++++++++++++++++++++++++++++++
          // You need to figure out what to do
       // END if there is a vowel
       // If the first character was upper, convert the new first character to upper
   // END if first is vowel
   // Return pigWord
}

Sample output:

$ ./a.out
[1] to process a file
[2] to process a single sentence
Your choice: 2
Your sentence: 
Make a quack sound for Bill and Jane, if you like ducks!
Akemay away ackquay oundsay orfay Illbay andway Anejay, ifway ouyay ikelay ucksday! 
$

Last night I was able to write the code to translate so qu will stay together and I was able to get the punctuation to stay in the correct place. However, I still cannot figure out the y.

In Pig Latin, here are the rules for y:
If y is being used a consonant, yellow -> ellowyay
If y is being used as a vowel, style -> ylestay

Here is my updated code that I wrote last night:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string getMenuOption();
string translateLine(string);
string translateWord(string);
string getNextWord(string, int);
bool isVowel(char);
bool isPuncutation(char);

// Main
int main()
{
    string sentence, pigSentence;
    string menuChoice;

    menuChoice = getMenuOption();
    if (menuChoice == "1")
    {
        ifstream inputFile;
        ofstream outputFile;
        inputFile.open("Program5Input.txt");
        outputFile.open("Program5Output.txt");
        while (inputFile >> sentence)
        {
        pigSentence = translateLine(sentence);
        outputFile << pigSentence;
        }

    }
    else
    {
        cout << "Your sentence: ";
        cout << endl;
        getline(cin, sentence);
        // call a function to translate a sentence
        pigSentence = translateLine(sentence);
        cout << pigSentence << endl;
    }

    system("pause");
    return 0;
}


// Menu
string getMenuOption()
{
    string menuChoice;


    cout << "[1] to process a file";
    cout << endl;
    cout << "[2] to process a single sentence";
    cout << endl;
    cout << "Your choice: ";
    getline(cin, menuChoice);

    return menuChoice;
}

// Translate Line Function
string translateLine(string sentence)
{
    int i = 0, length;
    length = sentence.length();
    string word, pigWord, pigSentence="";
    while (i < length)
    {
        word = getNextWord(sentence, i);
        i = i + word.length() + 1;
        pigWord = translateWord(word);
        pigSentence = pigSentence + pigWord + " ";
    }

    return pigSentence;
}

// Get Next Word Function
string getNextWord(string sentence, int i)
{
    string word;
    char letter;
    letter = sentence[i];
    while (letter != ' ' && i < sentence.length())
    {
        word = word + letter;
        i = i + 1;
        if (i < sentence.length())
            letter = sentence[i];
    }
    return word;
}

// Translate Word Function
string translateWord(string word)
{
    int i = 0;
    string pigWord;
    string first = "";
    string second = "";
    string punct = "";
    if (isVowel(word[0]) == true)
    {
        while (i < word.length() && word[i] != ',' && word[i] != '.' && word[i] != '!' && word[i] != '?')
        {
            first = first + word[i];
            i++;
        }
        while (i < word.length())
        {
            punct = punct + word[i];
            i++;
        }
        pigWord = first + "way" + punct;

    }

    else
    {
        while (i < word.length() && word[i] != 'a' && word[i] != 'e' && word[i] != 'i' && word[i] != 'o' && word[i] != 'u')
        {
            if (word[i] == 'q')
            {
                first = first + word[i] + word[i+1];
                i++;
            }

            else
            {
            first = first + word[i];
            i++;
            }
        }

        while (i < word.length() && word[i] != ',' && word[i] != '.' && word[i] != '!' && word[i] != '?')
        {

            if (word[i-1] == 'q')
            {
                i++;
                second = second + word[i];
                i++;
            }
            else
            {
                second = second + word[i];
                i++;
            }
        }

        while (i < word.length())
        {
            punct = punct + word[i];
            i++;
        }
        pigWord = second + first + "ay" + punct;
    }
    return pigWord;
}

// Determine Whether Character is a Vowel
bool isVowel(char letter)
{
    bool vowel;
    letter = tolower(letter);

    if (letter == 'a' || letter == 'e' || 
        letter == 'i' || letter == 'o' || 
        letter == 'u' )
    {
        vowel = true;
    }
    else 
    {
        vowel = false;
    }
    return vowel;
}

Here is an example of how I did punctuation using string functions.

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Find the punctuation ".?:;!,"
// Warning:  Not handling more than one punctuation in word
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
size_t punctPos(word.find_first_of(".?:;!,"));

// If the first letter is a vowel
if (isVowel(word[0]) == true) {
    string punct("");
    // If there is punctuation
    if ( punctPos != string::npos ){
       // Save off the punctuation
       punct.assign(1,word[punctPos]);
       // Remove punctuation
       word.erase(word.begin()+punctPos);
    }
    // Create new word using word, "way" and maybe pucntuation
    pigWord = word + "way"+punct;
}

Maybe for y we can try theses rules. It may not catch them all but something is better then nothing.

  1. If there is no vowel found, use y as a vowel (i.e. my, by, sky and etc...)
  2. If the only vowel is at the end and there is a y before it, use y as a vowel (i.e. style, Kyle and etc...)

On Y as a consonant or vowel:
What are the rules defining this? I don't know the rule.

The "rules" that I think will work in regards to this program are:

If y is followed or preceded by a consonant, it is being used as a vowel (style). If surrounded by a vowel, it is being used as a consonant. (yellow, yam, eye).

I can't figure out how to write code to check it, though. I wrote a sample code, but it doesn't display correctly at all . I know it isn't in the loop correctly because first is grabbing previously defined parameters on top of the added ones, and it gives me the output "yleeststylay" rather than "ylestay". When I put my information in a new group of variables, firstx and secondx, it displays correctly if I just tell it to cout << secondx << first, but I can't figure out how to write something that determines when pigWord = secondx + firstx + ay + punct; OR pigWord = secondx + firstx + ay + punct;

Any help would REALLY be appreciated.

My Code (just for the Translate Function, which is where the problem lies)

// Translate Word Function
string translateWord(string word)
{
    int i = 0;
    string pigWord;
    string first = "";
    string second = "";
    string punct = "";
    // If the word begins with a vowel

    if (isVowel(word[0]) == true)
    {
        while (i < word.length() && word[i] != ',' && word[i] != '.' && word[i] != '!' && word[i] != '?')
        {
            first = first + word[i];
            i++;
        }
        while (i < word.length())
        {
            punct = punct + word[i];
            i++;
        }
        pigWord = first + "way" + punct;

    }

    // If the word begins with a consonant
    else 
    {
        while (i < word.length() && word[i] != 'a' && word[i] != 'e' && word[i] != 'i' && word[i] != 'o' && word[i] != 'u')
        {
            if (word[i] == 'y' && isVowel(word[i-1]) == false && isVowel(word[i+1]) == false)
            {
                int j = 0;
                while (j < i)
                {
                    first = first + word[j];
                    j++;
                }
                int k = i;
                while (k >= i && k < word.length())
                {
                    second = second + word[k];
                    k++;
                }
            }


            if (word[i] == 'q')
            {
                first = first + word[i] + word[i+1];
                i++;
            }

            else
            {
            first = first + word[i];
            i++;
            }

        }

        while (i < word.length() && word[i] != ',' && word[i] != '.' && word[i] != '!' && word[i] != '?')
        {

            if (word[i-1] == 'q')
            {
                i++;
                second = second + word[i];
                i++;
            }
            else
            {
                second = second + word[i];
                i++;
            }
        }
        // If the word contains punctuation
        while (i < word.length())
        {
            punct = punct + word[i];
            i++;
        }

        pigWord = second + first + "ay" + punct;
    }
    return pigWord; 
}

Using your IF statement if (word[i] == 'y' && isVowel(word[i-1]) == false && isVowel(word[i+1]) == false)
if 'y' is the first letter, what is the value of word[i-1]?

Test this some more

// Translate Word Function
string translateWord(string word)
{
    int i = 0;
    string pigWord;
    string first = "";
    string second = "";
    string punct = "";
    // If the word begins with a vowel

    if (isVowel(word[0]) == true)
    {
        while (i < word.length() && word[i] != ',' && word[i] != '.' && word[i] != '!' && word[i] != '?')
        {
            first = first + word[i];
            i++;
        }
        while (i < word.length())
        {
            punct = punct + word[i];
            i++;
        }
        pigWord = first + "way" + punct;

    }
    // If the word begins with a consonant
    else 
    {
        while (i < word.length() && word[i] != 'a' && word[i] != 'e' && word[i] != 'i' && word[i] != 'o' && word[i] != 'u')
        {
            //----------------------------------------------------------------------
            // Added checks to make sure you don't index outside the memory of word
            if (word[i] == 'y' && i>=1 && isVowel(word[i-1]) == false && 
                    ( (i+1)==word.length() || ((i+1)<word.length() && isVowel(word[i+1]) == false) ) )
            {
                int j(0);
                //-------------------------------
                // Reset first
                first = "";
                while (j < i)
                {
                    first = first + word[j];
                    j++;
                }
                //-------------------------------
                // Get out of first while loop!
                break;
            }
            if (word[i] == 'q')
            {
                first = first + word[i] + word[i+1];
                i++;
            }
            else
            {
                first = first + word[i];
                i++;
            }
        }

        while (i < word.length() && word[i] != ',' && word[i] != '.' && word[i] != '!' && word[i] != '?')
        {
            if (word[i-1] == 'q')
            {
                i++;
                second = second + word[i];
                i++;
            }
            else
            {
                second = second + word[i];
                i++;
            }
        }
        // If the word contains punctuation
        while (i < word.length())
        {
            punct = punct + word[i];
            i++;
        }

        pigWord = second + first + "ay" + punct;
    }
    return pigWord; 
}

Output:

$ ./a.out
[1] to process a file
[2] to process a single sentence
Your choice: 2
Your sentence: 
Bill and Jane, have my style of quacking!
illBay andway aneJay, avehay ymay ylestay ofway ackingquay! 
$

You still need to fix capitals, Bill translate to illBay, should be Illbay and Jane should be Anejay.

@histrungalot, thank you SO much for your help! I had EXACTLY what you wrote the other day when I was playing around besides the break! I knew it had something to do with that while loop! Thank you so much for your help, I really appreciate it. And thank you to the rest of you who contributed to the thread as well!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.