通过引用调用和 ostream 以及递归的字符串操作
Call by reference and ostream and string manipulation recursive
全部!
我试图让我的程序正常工作,但我不认为它应该是递归的 该程序应该在文本文件中打印某些字符串并将它们放入文本框。
但是,我修改错了,搞了个死循环(居然是栈溢出,呵呵,好笑。。。)。任何帮助将非常感激。
这是原型的代码(是的,使用了#include):
void textBox(ostream & out, string text);
以及主函数和子程序中的部分:
cout << "Part c: textBox(text)" << endl ;
cout << "---------------------" << endl ;
ofstream fout("myFile.txt");
string box = "Text in a Box!";
textBox(fout, box);
fout << endl;
string size = "You can put any text in any size box you want!!";
textBox(fout,size);
fout << endl;
string courage = "What makes the muskrat guard his musk? Courage!";
textBox(fout,courage);
void textBox(ostream & out, string text)
{
// ---------------------------------------------------------------------
// This routine outputs text surrounded by a box.
// ---------------------------------------------------------------------
ofstream myFile("myFile.txt");
textBox(cout, "Message to screen");
textBox(myFile, "Message to file");
int textSize = text.size();
out << '+' << setfill('-') << setw(textSize+1) << right << '+' << endl;
out << '|' << text << '|' << endl;
out << '+' << setfill('-') << setw(textSize+1) << right << '+' << endl;
out << setfill(' ') ;
}
它是递归的,因为 textBox
调用它自己。如果您满足以下条件,您的程序将 运行:
A) 删除 textBox
中对 textBox
的两个调用
或 B) 使 textBox
像这样:
void textBox(ostream & out, string text)
{
int const textSize = text.size() + 1;
out << '+' << setfill('-') << setw(textSize) << right << '+' << endl;
out << '|' << text << '|' << endl;
out << '+' << setfill('-') << setw(textSize) << right << '+' << endl;
out << setfill(' ') ;
}
并创建另一个函数:
void textBoxWithInfo(ostream & out, string text)
{
textBox(cout, "Message to screen");
{ // Braces to deallocate + close myFile faster
ofstream myFile("myFile.txt");
textBox(myFile, "Message to file");
}
textBox(out, text);
}
并在main
中调用上述函数。
全部! 我试图让我的程序正常工作,但我不认为它应该是递归的 该程序应该在文本文件中打印某些字符串并将它们放入文本框。 但是,我修改错了,搞了个死循环(居然是栈溢出,呵呵,好笑。。。)。任何帮助将非常感激。 这是原型的代码(是的,使用了#include):
void textBox(ostream & out, string text);
以及主函数和子程序中的部分:
cout << "Part c: textBox(text)" << endl ;
cout << "---------------------" << endl ;
ofstream fout("myFile.txt");
string box = "Text in a Box!";
textBox(fout, box);
fout << endl;
string size = "You can put any text in any size box you want!!";
textBox(fout,size);
fout << endl;
string courage = "What makes the muskrat guard his musk? Courage!";
textBox(fout,courage);
void textBox(ostream & out, string text)
{
// ---------------------------------------------------------------------
// This routine outputs text surrounded by a box.
// ---------------------------------------------------------------------
ofstream myFile("myFile.txt");
textBox(cout, "Message to screen");
textBox(myFile, "Message to file");
int textSize = text.size();
out << '+' << setfill('-') << setw(textSize+1) << right << '+' << endl;
out << '|' << text << '|' << endl;
out << '+' << setfill('-') << setw(textSize+1) << right << '+' << endl;
out << setfill(' ') ;
}
它是递归的,因为 textBox
调用它自己。如果您满足以下条件,您的程序将 运行:
A) 删除 textBox
textBox
的两个调用
或 B) 使 textBox
像这样:
void textBox(ostream & out, string text)
{
int const textSize = text.size() + 1;
out << '+' << setfill('-') << setw(textSize) << right << '+' << endl;
out << '|' << text << '|' << endl;
out << '+' << setfill('-') << setw(textSize) << right << '+' << endl;
out << setfill(' ') ;
}
并创建另一个函数:
void textBoxWithInfo(ostream & out, string text)
{
textBox(cout, "Message to screen");
{ // Braces to deallocate + close myFile faster
ofstream myFile("myFile.txt");
textBox(myFile, "Message to file");
}
textBox(out, text);
}
并在main
中调用上述函数。