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;
}

Recommended Answers

All 5 Replies

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

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.

What does the warning say when you try to compile?

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.