【发布时间】:2015-11-05 04:34:22
【问题描述】:
全部! 我试图让我的程序正常工作,但我认为它不应该是递归的 该程序应该在文本文件中打印某些字符串并将它们放在一个文本框中。 但是,我修改错了,做了一个无限循环(其实是堆栈溢出,哈哈,好笑……)。任何帮助将非常感激。 这是原型的代码(是的,使用了#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,你都会一遍又一遍地调用它…… -
它是递归的,因为
textBox调用自己。 myFile.txt 的预期输出到底是什么?您的意思是在textBox中调用不同的函数吗?也许你根本不应该在那里调用函数。 -
字符串“String in a Box”等应该由textBox输出到文本文件中。
-
太基础了:“为什么我的递归调用会导致递归调用?”
标签: c++ recursion reference overflow call