C++ ifstream ofstream实现?

Sim*_*syy -1 c++ ifstream ofstream

我一直在编写这个Pig Latin程序,将英语转换为猪拉丁语,我一直在实现读取文件然后将其输出到另一个txt文件时遇到问题.

这是我到目前为止,但它不会编译因为

outputFile << pigLatin(englishWord) << ' ';
Run Code Online (Sandbox Code Playgroud)

这是源代码.有什么建议让它工作?谢谢

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;
}
Run Code Online (Sandbox Code Playgroud)

Nem*_*ric 7

您的pigLatin函数具有void返回类型,因此它不会返回您可能写入的任何内容ofstream:

outputFile << pigLatin(englishWord) << ' ';
Run Code Online (Sandbox Code Playgroud)

更改pigLatinto 的返回类型std::string并添加

 return piglatinWord; 
Run Code Online (Sandbox Code Playgroud)

在功能的最后:

string pigLatin(string englishWord)
{
   // ... implementation here
   return piglatinWord;
}
Run Code Online (Sandbox Code Playgroud)

另外,为了使你的功能更加清晰和统一,我建议你cout << piglatinWord;从函数中删除并输出调用函数中的返回值:

 englishWord = engWords.substr(0, location); 
 cout << pigLatin(englishWord); 
Run Code Online (Sandbox Code Playgroud)