Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <iostream>
#include <string>

using namespace std;

string getWord (string &);
string translatePigLatin (string);
string trim(string);

{
    string str; //user entered sentence
    string PigLatin; //translated sentence 
    
    //get Input
    cout<<"Enter a sentence that you want translated into Pig Latin";
    getline(cin, str);
    
    //Translate
    while(str.size() > 0)
    {
        //get the word from the sentence
        string word = getword(str);
        
        //translate word to pig latin 
        word = translatePigLatin(word);
        
        //add word to our translated string
        pigLatin += word + ' ';
    }
    
    //display the translation
    cout << pigLatin << endl;
    get char();
    return 0;
    
}

string getword(string &str);
{
    //trim any spaces from the beginning of the string
    str = trim(str);
    
    //get the beginning of the word by locating the first spaces
    //or until the end of the string is reached
    int i = 0;
    while (str[i] !=' '&& i < str.size())
        i++;
    
    //i now equals the length of the word
    //so copy the beginning the string from the beginning to the i
    string word = str.subtr(0,i);
    
    str.erase(0,i);
    
    return word;
}

string trim(string str);
{
    //erase 1 character at index 0 if it is a space 
    while (str[0] == ' ')
        str.erase(0,1);
        
    return str;
}

string translatePigLatin(string word)
{
    //get the first character
    char first = word [0];
    
    //append the letter to the end of the word
    word append(1, first);
    
    //append ay to the end of the word
    word.append("ay");
    
    //delete the first character
    word.erase(0, 1);
    
    return word;
}

What I have tried:

<pre>#include <iostream>
#include <string>

using namespace std;

string getWord (string &);
string translatePigLatin (string);
string trim(string);

{
    string str; //user entered sentence
    string PigLatin; //translated sentence 
    
    //get Input
    cout<<"Enter a sentence that you want translated into Pig Latin";
    getline(cin, str);
    
    //Translate
    while(str.size() > 0)
    {
        //get the word from the sentence
        string word = getword(str);
        
        //translate word to pig latin 
        word = translatePigLatin(word);
        
        //add word to our translated string
        pigLatin += word + ' ';
    }
    
    //display the translation
    cout << pigLatin << endl;
    get char();
    return 0;
    
}

string getword(string &str);
{
    //trim any spaces from the beginning of the string
    str = trim(str);
    
    //get the beginning of the word by locating the first spaces
    //or until the end of the string is reached
    int i = 0;
    while (str[i] !=' '&& i < str.size())
        i++;
    
    //i now equals the length of the word
    //so copy the beginning the string from the beginning to the i
    string word = str.subtr(0,i);
    
    str.erase(0,i);
    
    return word;
}

string trim(string str);
{
    //erase 1 character at index 0 if it is a space 
    while (str[0] == ' ')
        str.erase(0,1);
        
    return str;
}

string translatePigLatin(string word)
{
    //get the first character
    char first = word [0];
    
    //append the letter to the end of the word
    word append(1, first);
    
    //append ay to the end of the word
    word.append("ay");
    
    //delete the first character
    word.erase(0, 1);
    
    return word;
}
Posted
Updated 30-Apr-21 19:37pm

You have a block with no function header that should be there.
C
string getWord (string &);
string translatePigLatin (string);
string trim(string);

---- What goes on this line? ----
{
    string str; //user entered sentence
    string PigLatin; //translated sentence 
    
    //get Input
    cout<<"Enter a sentence that you want translated into Pig Latin";
    getline(cin, str);
    
    //Translate
    while(str.size() > 0)
    {
        //get the word from the sentence
        string word = getword(str);
        
        //translate word to pig latin 
        word = translatePigLatin(word);
        
        //add word to our translated string
        pigLatin += word + ' ';
    }
    
    //display the translation
    cout << pigLatin << endl;
    get char();
    return 0;
    
}
 
Share this answer
 
You left out the header for main().
int main() // before the first { in the program


You also spelled a function name as "getWord" in the prototype and as "getword" in the function definition.

And you have a (;) semicolon at the end of the function definition for "getword". That makes it another prototype and the {} braced block that follows will get an error.

And I see a statement "get char();" at the end of main() that is not C++ syntax of any sort. Delete or comment that out until you know what really belongs there.

That's all I see on the first pass. Fix those and *carefully* look at the code near the first error next time you compile. The compiler error message isn't always helpful, so you need to develop the habit of reading your own code critically. I said "first error" because one error can produce several messages at different lines, as the compiler tries to "fix up" the error to continue compiling. Usually the first message is a real error, but others may be phantom messages that will disappear when you fix an earlier error.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900