C++ 循环重复绘图故障
我试图得到一个循环来画圣诞树,但输出是错误的,我试图寻找答案,但我似乎找不到它,我被难住了。答案可能很明显,但我错过了很多,非常感谢您的帮助C++ 循环重复绘图故障,c++,C++,我试图得到一个循环来画圣诞树,但输出是错误的,我试图寻找答案,但我似乎找不到它,我被难住了。答案可能很明显,但我错过了很多,非常感谢您的帮助 #include <iostream> #include <assert.h> #include <iomanip> using namespace std; const char blank = ' '; const char leaf = '#'; const char wood = '|'; const int
#include <iostream>
#include <assert.h>
#include <iomanip>
using namespace std;
const char blank = ' ';
const char leaf = '#';
const char wood = '|';
const int minSize = 4;
const int maxSize = 20;
int treeHeight;
int& getValidHeight(int&);
void drawALineOfFoliage(int);
void drawFoliage(int);
void drawTrunk(int);
void drawAXmasTree(int);
void drawAXmasTree(int treeHeight) {
getValidHeight(treeHeight);
drawFoliage(treeHeight);
drawTrunk(treeHeight);
}
int& getValidHeight(int& treeHeight) {
cout << ("Please enter the size of the tree (4-20):\n");
cin >> treeHeight;
while ((treeHeight < minSize) || (maxSize < treeHeight)) {
cout << "ERROR: Invalid height! Enter the size of the tree (4-20):\n";
cin >> treeHeight;
return treeHeight;
}
}
void drawALineOfFoliage(int treeHeight) {
for (int x = 0; x < treeHeight; ++x){
for (int y = treeHeight; y > x; --y){
cout << blank;}
for (int y = 0; y < x; ++y){
cout << leaf;}}}
void drawFoliage(int treeHeight) {
int branchLine = 1;
do {
drawALineOfFoliage(treeHeight);
branchLine += 1;
} while (branchLine <= (treeHeight - 2));}
void drawTrunk(int treeHeight) {
int trunkLine(1), spaces;
while (trunkLine <= 2) {
spaces = 1;
while (spaces <= (treeHeight - 3)) {
cout << blank;
spaces += 1;}
cout << wood << "\n";
trunkLine += 1;
}
}
int main()
{
drawAXmasTree(treeHeight);
system("pause");}
#包括
#包括
#包括
使用名称空间std;
常量字符空白=“”;
常量char leaf='#';
常量char wood=“|”;
const int minSize=4;
常量int maxSize=20;
int树高;
int&getValidHeight(int&);
无效提款保函(内部);
空抽叶(int);
空抽屉(内部);
空抽轴承(内部);
空心牵引轴(内部树高){
getValidHeight(树型);
落叶(树高);
拉杆箱(树型);
}
int&getValidHeight(int&treeHeight){
树高;
while((树高x;--y){
cout您的lineofoliage
似乎为所有x
绘制线条。在我看来,树叶绘图代码的基本结构应该是:
drawFoliage(height) {
for(width = 0..height) cout << centeredLine(width);
}
drawCenteredLine(width) {
return blanks + leafs;
}
Drawleage(高度){
对于(width=0..height)cout,因此我获取了您的示例代码并对其进行了运行
首先,您的样式和制表符不一致,这会使代码很难阅读
接下来,您的drawalineofliage
实际上是在绘制整个树,而不是只绘制一行。因此,您缺少了一个cout'output is error'以及显示正确的输出和您得到的输出:int&getValidHeight(int&treehight)
为什么在这里使用引用?只需阅读输入并返回一个值。啊-那最好添加“家庭作业”标签。谢谢,我会确保我的格式在将来更易于理解。我已经阅读了你所说的内容和你提出的建议,我知道我错在哪里了,谢谢你的帮助非常感谢!@h3rcul35很高兴能为您提供帮助!
#
##
###
####
for (int y = 0; y < x*2; ++y) {
cout << leaf;
}
do {
drawALineOfFoliage(treeHeight);
branchLine += 1;
} while (branchLine <= (treeHeight - 2));
while (spaces <= (treeHeight - 3))
Please enter the size of the tree (4-20):
6
##
####
######
########
##########
||
||
Please enter the size of the tree (4-20):
7
#
###
#####
#######
#########
###########
#############
|||
|||
#include <iostream>
#include <assert.h>
#include <iomanip>
using namespace std;
const char blank = ' ';
const char leaf = '#';
const char wood = '|';
const int minSize = 4;
const int maxSize = 20;
int treeHeight;
int& getValidHeight(int&);
void drawALineOfFoliage(int);
void drawFoliage(int);
void drawTrunk(int);
void drawAXmasTree(int);
void drawAXmasTree(int treeHeight) {
getValidHeight(treeHeight);
drawFoliage(treeHeight);
drawTrunk(treeHeight);
}
int& getValidHeight(int& treeHeight) {
cout << ("Please enter the size of the tree (4-20):\n");
cin >> treeHeight;
while ((treeHeight < minSize) || (maxSize < treeHeight)) {
cout << "ERROR: Invalid height! Enter the size of the tree (4-20):\n";
cin >> treeHeight;
}
return treeHeight;
}
void drawALineOfFoliage(int treeHeight) {
for (int x = 0; x < treeHeight; ++x) {
for (int y = treeHeight; y > x; --y) {
cout << blank;
}
for (int y = 0; y < x*2; ++y) {
cout << leaf;
}
cout << endl;
}
}
void drawALineOfFoliageOdd(int treeHeight) {
for (int x = 0; x < treeHeight; ++x) {
for (int y = treeHeight; y > x; --y) {
cout << blank;
}
cout << leaf;
for (int y = 0; y < x*2; ++y) {
cout << leaf;
}
cout << endl;
}
}
void drawFoliage(int treeHeight) {
int branchLine = 1;
drawALineOfFoliageOdd(treeHeight);
do {
branchLine += 1;
} while (branchLine <= (treeHeight - 2));
}
void drawTrunk(int treeHeight) {
int trunkLine(1), spaces;
while (trunkLine <= 2) {
spaces = 1;
while (spaces <= (treeHeight - 1)) {
cout << blank;
spaces += 1;
}
cout << wood << wood << wood << endl;
trunkLine += 1;
}
}
int main() {
drawAXmasTree(treeHeight);
system("pause");
}