C++ C++;-使用递归函数创建金字塔时出错
所以我一直在想为什么这不起作用。基本上,我试图编写一个递归函数,在控制台中显示一个漂亮的文本金字塔 用户首先输入高度,然后输入用于创建金字塔的符号,该函数在此处调用:C++ C++;-使用递归函数创建金字塔时出错,c++,function,recursion,C++,Function,Recursion,所以我一直在想为什么这不起作用。基本上,我试图编写一个递归函数,在控制台中显示一个漂亮的文本金字塔 用户首先输入高度,然后输入用于创建金字塔的符号,该函数在此处调用: //_pyramidHeight is 10 //The second int is to specify the beginning width, which should be the point at the top. pyramidLine(_pyramidHeight, 1); 我创建的函数如下所示: void
//_pyramidHeight is 10
//The second int is to specify the beginning width, which should be the point at the top.
pyramidLine(_pyramidHeight, 1);
我创建的函数如下所示:
void pyramidLine (int _height, int _width)
{
for (; _height > 0; _height--, _width + 2)
{
cout << setfill (' ') << setw(_height - 1);
cout << setfill (_pyramidBase) << setw(_width);
pyramidLine (_height, _width);
}
return;
}
void棱锥体线(整数高度,整数宽度)
{
对于(;_高度>0;_高度-,_宽度+2)
{
CUT简单:你从来没有碰到过一个基本的情况。你有无限的递归,因为<代码>高度> -/COD>在你的<代码>每次迭代之后被评估为< /Cult>循环,而不是以前。考虑<代码>金字塔线(
任何可以递归执行的操作,也可以迭代执行。迭代不会像递归那样导致堆栈溢出。请尝试使用迭代重新编写它。我忘了提到,但我的教授说,它在金字塔创建中使用的函数必须是递归的。