I have been writing this Pig Latin program to convert english to pig latin, and I have been having issues implementing the read in file and then subsequently outputing it to another txt file.
This is what I have so far, but it wont compile because of
outputFile << pigLatin(englishWord) << ' ';
Here is the source code. Any suggestion to get it working? Thanks
void pigLatin(string englishWord)
{
string piglatinWord;
bool truefalse = false;
int letter = 0, wordLength = englishWord.length( );
while (!truefalse && letter < wordLength)
{
if (englishWord.substr(letter,1) == "a" || englishWord.substr(letter,1) == "e" ||englishWord.substr(letter,1) == "i" || englishWord.substr(letter,1) == "o" || englishWord.substr(letter,1) == "u")
truefalse = true;
else
letter++;
}
if (letter > wordLength)
piglatinWord = englishWord + "-way ";
else
piglatinWord = englishWord.substr(letter, wordLength-letter) + englishWord.substr(0,letter)+ "-ay " ;
cout << piglatinWord;
}
int main( )
{
ifstream inputFile;
inputFile.open("PigLatinIn.txt");
ofstream outputFile;
outputFile.open("PigLatinOut.txt");
string englishWord, engWords;
bool done = false;
int location;
while(!inputFile.eof())
{
string englishWord;
inputFile >> englishWord;
outputFile << pigLatin(englishWord) << ' ';
}
{
while (!done)
{
location = engWords.find(" ");
if (location == -1)
{
done = true;
location = engWords.length( );
}
englishWord = engWords.substr(0, location);
pigLatin(englishWord);
if (!done)
engWords = engWords.substr(location + 1, engWords.length( ) - location + 1);
}
}
cout << endl;
inputFile.close();
outputFile.close();
return 0;
}
Your pigLatin
function has void
for the return type, so it is not returning anything that you might write into the ofstream
:
outputFile << pigLatin(englishWord) << ' ';
Change the return type of pigLatin
to std::string
and add
return piglatinWord;
at the end of the function:
string pigLatin(string englishWord)
{
// ... implementation here
return piglatinWord;
}
Also, to make your function more clear and uniform, I would suggest you to remove cout << piglatinWord;
from the function and output the return value in the caller function:
englishWord = engWords.substr(0, location);
cout << pigLatin(englishWord);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.