Couple of issues:
This appends the first letter of the word onto the end.
word += word[0];
Since I don't know what "PigLatin" is I can't tell if this is correct. A comment about what is the expected translation is really required so we can understand if this is correct behavior.
This sets the first letter to 0
.
word[0] -= word[0];
Note: NOT the character 0
but the number zero which is an unprintable character (there are no glyphs that represent the number 0
). Note the character 0
is represented by the number 48 (in ASCII or UTF-8).
Did you want to remove the first character from the string?
word = word.substr(1); // Gets the substring from 1 to the end.
// Assigns it to the variable word.
Adds the string "ay" to the end of word.
word += "ay";
Sure. Sounds reasonable. But need to understand the expected behavior/
You have a string. Which you are converting into a sequence of words and storing in v
.
std::vector<std::string> v;
But the only thing you do with v
is loop over it and get each word to call translate()
with. Why not remove the middle man and not use the container at all.
Here You are creaging a loop as long as the string. But each iteration you are taking a word from the string.
for(int i = 0; i < str.length(); i++){
So this will have a lot of blank words on the end. A better way is to read words from the stream until there are no more words in the stream.
while (stringStream >> currentWord) {
// Do stuff with currentWord
}
Avoid the use of std::endl
.
std::cout << std::endl;
Its probably not doing any harm here. But its a bad habit. The difference between \n
and std::endl
is that std::endl
also flushes the buffer. Manually forcing a buffer flush is usually incorrect. The buffers will auto flush at the optimal time. Program inserted flushing is usually wrong and is a major cause of eniffciency in C++ code.
I would refactor the inner loop like this:
while (stringStream >> currentWord) {
std::cout << translate(std::move(currentWord)) + " ";
}
std::cout << "\n";
Notice the std::move()
this moves an object to the function (saves a copy). So at the translate()
needs to be changed slightly.
std::string translate(std::string&& word)
{ ^^ bind a moved parameter.
// stuff.
return word;
}
word[0] -= word[0];
: AHHHHHHHHHHHH!!! \$\endgroup\$main()
and the processing of the string should be in another function. That other function should return astring
which then can callcout
inmain
. This way the code is usable in other contexts. \$\endgroup\$