I'm trying to write a function that will cut off the last two letters of 1 word and then add the remainder in front of another word. I keep getting this extremely long error when I try to compile and my guess is because I'm implementing the erase string wrong.
1 2 3 4 5 6 7 8 9 10 11 12
Call
EnglishWord = Translate(FirstSide, SecondSide);
Function
void Translate(string word, string piglatin)
{
string Translated;
int plen = piglatin.length();
piglatin.erase(plen-1, 2);
Translated = piglatin + word;
}
#include <algorithm>
#include <string>
#include <iostream>
std::string mangle(std::string s)
{
if ( s.length() > 2 )
std::rotate(s.begin(), s.begin()+2, s.end()) ;
// TODO: If the last letter in s is not a vowel, then...
return s ;
}
int main()
{
std::cout << mangle("pig") << ' ' << mangle("latin") << '\n' ;
}
I just did:
string Translate(string word, string piglatin)
{
string Translated;
int plen = piglatin.length();
piglatin.erase(piglatin.end() - 2, piglatin.end());
Translated = piglatin + word;
return Translated;
}
but there appears to be a new problem. I have it so that it reads in the first word with a getline until a '-' is encountered and the second word until a ' ' is encountered. It works fine until the end of a line in which it adds an extra space and splits the next word.
this
is
a
little
test
to
see
h
ow
the
pig
latin
translation
pr
ogram
waorks
I tried making a while loop so that if the first word read in was a '\n' it would just read in another word, but then I realized that the \n would be on the second word of the last line. Is there a way to check to see if the \n is tacked onto the end?
Initially I read in each piglatin word as 1 word, but then translating them back to English became a real problem for me, because it was hard for me to distinguish between each side of the '-'. I'm trying to use the string.find function to search the second side of the piglatin word for a '\n' right after its read in and to remove it before I put the word through any of my functions. I'm not quite sure how the string.find function works though.
I'm starting to think I don't know the real problem with why the output comes out the way it does. I know it has something to do with the new line feed. But from looking at my code it would seem that instead of extra end lines the "ay" I'm trying to remove from each word would remain. At this point I could really use some help.
This is what I'm working with at:
Again, you would find this simpler if you just used cin >> token and split the token apart.
If I were you, the code above would become:
1 2
while ( input >> token )
WordArray[Count++] = Translate(token) ;
and Translate would deal with breaking down the token into parts and returning the translated string. (The translation can be accomplished by adding two substrings of token together)
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
usingnamespace std ;
string Translate(const string& token)
{
string::size_type pos = token.find('-') ;
if ( pos == string::npos )
return token ;
// left is text before the '-'
string left = token.substr(0, pos) ;
// right is text after the '-' minus the 2 characters at the end of token.
string right = token.substr(pos+1, token.size()-(pos+1)-2) ;
return right + left ;
}
int main()
{
istringstream input("is-Thay is-ay a-ay ittle-lay est-tay o-tay ee-say\n""ow-hay e-thay ig-pay atin-lay anslation-tray\n""ogram-pray orks-way" ) ;
vector<string> words ;
string token ;
while ( input >> token )
words.push_back(Translate(token)) ;
for ( unsigned i=0; i<words.size(); ++i )
std::cout << words[i] << '\n' ;
}