2017-12-11 4 views
-3

私はクリスマスツリーを描くためにループを作成しようとしていますが、出力が間違っています。私は答えを探してみましたが、見つけられないと思われます。答えは明白かもしれませんが、私はたくさんお見逃ししており、どんな助けも大歓迎です!ループの繰り返し描画のトラブルのため

#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");} 

出力はちょうどクリスマスツリー解体であるので、すべての レベルが同じライン上にあり、数回繰り返し

+1

'出力が間違っています'正しい出力と出力を表示 –

+0

OT: 'int&getValidHeight(int&treeHeight)'なぜここで参照を使用しますか?入力を読み込んで値を返すだけです。 –

+1

ああ - 「宿題」というタグを追加するとよいでしょう。 – xtofl

答えて

1

私はあなたのサンプルコードを取って実行しました。

まず、あなたのスタイリングとタブの矛盾は、コードを実際に読みにくくする可能性があります。

次に、drawALineOfFoliageは、実際には、単なる行ではなくトランクなしでツリー全体を描画しています。したがって、他の2つのネストされたforループの後でメインのforループ内にcout << endl;がありませんでした。

は、編集を開始します

編集:私は半分の木について話を忘れていました。

既存のコードでは、ツリーの半分しか印刷されません。このようなもの...

# 
    ## 
### 
#### 

半分のクリスマスツリーです。実際のツリーと似ているようにするために、葉を印刷するforループに*2を追加していました。 (cout << leaf << leaf;でもかまいません)

for (int y = 0; y < x*2; ++y) { 
    cout << leaf; 
} 

終了編集。

あなたdrawALineOfFoliageはループが(トランクなしで、再び)樹木の量をループしているので、それを削除する必要がありながら、これを行う

drawFoliageで、すでに

do { 
    drawALineOfFoliage(treeHeight); 
    branchLine += 1; 
} while (branchLine <= (treeHeight - 2)); 
をツリーを印刷しているので。

これで上の部分が完成しましたので、トランクを見てみましょう。

while (spaces <= (treeHeight - 3)) 

- 3はどこからも出て来ないようです。そしてそれはちょうど私が- 3を取り除き、代わりに2森を印刷させるようにちょっと奇妙に見える1行につき1つだけ|を印刷しています。

今、出力は

Please enter the size of the tree (4-20): 
6 

    ## 
    #### 
    ###### 
    ######## 
########## 
    || 
    || 

その大丈夫、まだ変な...多少のようになります。 TLDR、私はいくつか微調整を行い、このような最終結果を得ました...

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"); 
} 

注:これで不要なコードがたくさんあるので、私はクリーンアップの任意の並べ替えをしませんでした。私はちょうど私が得た最高のソリューションを提供するために必要なものの上に取り組んだ。

+1

ありがとうございます、私は私の書式を今後より理解しやすくするために取り組んでいます。私はあなたが言ったこととあなたがした勧告を通り過ぎてきました。どこが間違っているのかを見て、あなたの助けに感謝します。 – h3rcul35

+0

@ h3rcul35助けになるのはうれしい! – BinHong

0

あなたlineOfFoliageはすべてx < heightの行を行うようです。

drawFoliage(height) { 
    for(width = 0..height) cout << centeredLine(width); 
} 

drawCenteredLine(width) { 
    return blanks + leafs; 
} 
関連する問題