Write a program that reads a sentence as input and converts each word to “Pig Latin.” To convert an English word to Pig Latin, you remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word. Here is an example of the conversion to Pig Latin:

I need help with the code that I have written. I am getting errors with 'int i' in void pigLatin(). Please need your feedback and if I need to make any changes, please tell me. Appreciated your help.

#include <iostream>
#include <string>

using namespace std;

void pigLatin();
string PLW(string); //PLW for "Pig Latin Wod"

int main ()

{
        cout << "     Pig Latin Translator     " << endl;
        cout << "-----------------------------\n" << endl;

}
 void pigLatin()
{
        string entry= " ", PLatin= " ";
        string entSent= " ";

        cout <<" Please enter a sentence below! \n" << "English: " << endl;
        getline(cin, entry);
        int i = entry.find (' ');
        while ( i < entry.length()-1)
        {
            entSent = entry.substr(0, i);

            entry = entry.substr(i + 1); //takes the 1st letter

            PLatin += PLW(entSent)+ " "; //translates the word and adds to collected pig latin phrase

            i = entry.find(' '); //find next space
        }
        PLatin+=PLW(entry);
        cout << "\n";
        cout << "Pig Latin: " <<PLatin;
}

string PLW (string r) // accepts word in English, returns word in pig latin
{
    string ans= " ", prefix= " ";
    for (int j=0; j < r.length(); j++)
    {
        ans = r.substr(prefix.length());
        ans+= prefix + "ay";
}
    return ans;
}

find returns a size_t type or string::npos not an int. You might want to look at stringstream(<sstream>). This will allow to use the extraction operator(>>) which will automatically find the next space.

Also your algorithm is incomplete. If you search online you can find several examples of the rules for Pig Latin

Edited 1 Year Ago by tinstaafl

okay so, i added long i instead. and also i wrote int main properly. now when I compliple, i get warning for cout <<" Please enter a sentence below! \n" << "English: " << endl;
can you tell me what is wrong there.

#include <iostream>
#include <string>

using namespace std;

void pigLatin();
string PLW(string); //PLW for "Pig Latin Wod"

int main ()
{
        char again = '1';
        cout << "     Pig Latin Translator     " << endl;
        cout << "-----------------------------\n" << endl;

        while (again=='1')
        {
            pigLatin();
            cout << "\n\nType 1 to do another one: "<< endl;
            cin >> again;
            cin.ignore(90,'\n');
        }
    return 0;
}
   void pigLatin()
{
        string entry= " ", PLatin= " ";
        string entSent= " ";

        cout <<" Please enter a sentence below! \n" << "English: " << endl;
        getline(cin, entry);
        long i  = entry.find (' ');
        while ( i < entry.length()-1)
        {
            entSent = entry.substr(0, i);

            entry = entry.substr(i + 1); //takes the 1st letter

            PLatin += PLW(entSent)+ " "; //translates the word and adds to collected pig latin phrase

            i = entry.find(' '); //find next space
        }
        PLatin+=PLW(entry);
        cout << "\n";
        cout << "Pig Latin: " <<PLatin;
}

string PLW (string r) // accepts word in English, returns word in pig latin
{
    string ans= " ", prefix= " ";
    for (int j=0; j < r.length(); j++)
    {
        ans = r.substr(prefix.length());
        ans+= prefix + "ay";
}
    return ans;
}

I speak pig latin -> Igspay atinlay. My sister and I spoke it extensively as children in the 1950's. FWIW, this is where you want a finite state machine representation of the transformations needed in order to handle this in a generic manner. Have you studied finite state machines as yet?

no I havnt. I am in my sencond year of college and learning programming as you can see. Will you able to help me with the problem am having in the code above.

Edited 1 Year Ago by Riya_2: grammer mistake

This article has been dead for over six months. Start a new discussion instead.