[英]C++ ifstream ofstream implementation?
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. 我一直在编写此Pig Latin程序以将英语转换为Pig Latin,并且在实现读入文件然后将其输出到另一个txt文件时遇到了问题。
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
: 您的
pigLatin
函数的返回类型void
,因此不会返回您可能写入ofstream
任何内容:
outputFile << pigLatin(englishWord) << ' ';
Change the return type of pigLatin
to std::string
and add 将
pigLatin
的返回类型更改为std::string
并添加
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;
另外,为使您的功能更加清晰和统一,我建议您删除
cout << piglatinWord;
from the function and output the return value in the caller function: 从该函数并在调用函数中输出返回值:
englishWord = engWords.substr(0, location);
cout << pigLatin(englishWord);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.