2016-08-13 124 views
-2

新的C++所以如果我得到任何錯誤的話請不要嘲笑我。我正在嘗試編寫一個程序,該程序會詢問用戶單元,然後使用函數繪製房屋。我遇到的問題是在我的drawcone函數中這是我迄今的進展。初學者C++畫房子程序

#include<iostream> 
using namespace std; 

void drawcone(int height); 
void drawHorizontalLine(int numXs); 
void draw2VerticalLines(int numSpaces, int numRows); 
void drawOneRow(int numSpaces); 
void getDimensions(int& width, int& height); 
void drawbox(int width, int height); 

int main(){ 

int width; 
int height; 

    getDimensions(height, width); 
    drawcone(height); 
    drawHorizontalLine(width);  
    draw2VerticalLines(width - 2, height - 2); 
    drawHorizontalLine(width); 



return 0; 
} 

void drawbox(int width, int height){ 

    drawHorizontalLine(width);  
    draw2VerticalLines(width - 2, height - 2); 
    drawHorizontalLine(width); 

} 

void drawcone(int height){ 

    int base = height * 2; 

    int r = 0; 

    while (r != height){ 

     int c = 0; 
     while (c != base){ 

      if(c==height-r || c==height+r) 
       cout << "X"; 
      else 
      cout << " "; 
      c++; 

} 
    cout << endl; 
    r++; 
} 
} 

void drawHorizontalLine(int numXs) 
    {    
     int count; 

     for (count = 0; count < numXs; count++){ 
      cout << "X"; 
     } 
     cout << endl; 
    } 


void draw2VerticalLines(int numSpaces, int numRows) 
    {       
     int rowCount; 

     for (rowCount = 0; rowCount < numRows; rowCount++){ 
      drawOneRow(numSpaces); 
     } 
    } 

void drawOneRow(int numSpaces) 
    { 
     int spaceCount; 

     cout << "X"; 
     for (spaceCount = 0; spaceCount < numSpaces; spaceCount++){  
      cout << " "; 
     } 
     cout << "X" << endl; 
    } 

void getDimensions(int& width, int& height){ 

cout << "Enter the width of the house" << endl; 
cin >> width; 

cout << "Enter the height of the house" << endl; 
cin >> height; 

} 

正確的樣本輸出應該是這樣的

X 
    X X 
X X 
XXXXX 
X X 
X X 
XXXXX 

我的電流輸出看起來像這樣

X 
    X X 
    X X 
X  X 
XXXX 
X X 
X X 
XXXX 

我想錐體要稍微小,因此這將是相稱盒子。我也更喜歡一個不涉及修改drawbox函數的答案。感謝您的時間!

+1

是什麼通過與調試代碼加強告訴你的原因造成的問題? –

+1

當你調用getDimensions時,你的寬和高的順序是錯誤的,所以這些值會被翻轉。 – cambunctious

+0

另一個問題是,你認爲屋頂的底部是高度的兩倍,而對正確輸出的一小部分可以看出它不是。你已經知道屋頂應該有多寬 - 高度不得而知。 (但你不需要計算它的高度。) – molbdnilo

回答

0

這裏有一個解決方案。但是,只有當寬度是奇數時,錐體纔是對稱的。

#include<iostream> 
using namespace std; 

void drawcone(int height); 
void drawHorizontalLine(int numXs); 
void draw2VerticalLines(int numSpaces, int numRows); 
void drawOneRow(int numSpaces); 
void getDimensions(int& width, int& height); 
void drawbox(int width, int height); 

int main() { 

    int width; 
    int height; 

    getDimensions(width, height); 
    drawcone(width); 
    drawHorizontalLine(width); 
    draw2VerticalLines(width - 2, height - 2); 
    drawHorizontalLine(width); 

    return 0; 
} 

void drawbox(int width, int height) { 

    drawHorizontalLine(width); 
    draw2VerticalLines(width - 2, height - 2); 
    drawHorizontalLine(width); 
} 

void drawcone(int width) { 

    for (int i=0; i<(width/2 + width%2); i++) { 

     for (int j=width/2-i; j>0; j--) { 
      cout << " "; 
     } 

     drawOneRow(i*2-1); 
    } 
} 

void drawHorizontalLine(int numXs) 
{ 
    int count; 

    for (count = 0; count < numXs; count++) { 
     cout << "X"; 
    } 
    cout << endl; 
} 


void draw2VerticalLines(int numSpaces, int numRows) 
{ 
    int rowCount; 

    for (rowCount = 0; rowCount < numRows; rowCount++) { 
     drawOneRow(numSpaces); 
    } 
} 

void drawOneRow(int numSpaces) 
{ 
    int spaceCount; 

    cout << "X"; 
    if (numSpaces > 0) { 
     for (spaceCount = 0; spaceCount < numSpaces; spaceCount++) { 
      cout << " "; 
     } 
     cout << "X"; 
    } 
    cout << endl; 
} 

void getDimensions(int& width, int& height) { 

    cout << "Enter the width of the house" << endl; 
    cin >> width; 

    cout << "Enter the height of the house" << endl; 
    cin >> height; 

} 
0

應該很容易調試,它打印出房屋「屋頂」的空間數量錯誤。 這個問題應該包含在while (r != height)塊 嘗試用else {cout << " ";}搞亂else if(c%2==1){cout << " ";}

+0

林不知道你的意思。我還是新的,你能解釋一下嗎? – snophog

0
#include<iostream> 

using namespace std; 

void roof(int height); 
void hLine(int num); 
void vLine(int spaces, int rows); 
void row(int spaces); 
void dimensions(int& width, int& height); 
void box(int width, int height); 

int main() 
{ 
    int width; 
    int height; 
    dimensions(height, width); 
    roof(height); 
    hLine(width);  
    vLine(width - 2, height - 2); 
    hLine(width); 

    return 0; 
} 
void box(int width, int height) 
{ 
    hLine(width);  
    vLine(width - 2, height - 2); 
    hLine(width); 
} 
void roof(int height) 
{ 
    int base = height * 2; 
    int r = 0; 
    while (r != height) 
{ 
    int c = 0; 
    while (c != base) 
    { 
     if(c==height-r || c==height+r) 
      cout << "*"; 
     else 
     cout << " "; 
     c++; 
    }  
    cout << endl; 
    r++; 
} 
} 
void hLine(int num) 
{    
    int count; 
    for (count = 0; count < num*2+1; count++) 
    { 
     cout << "-"; 
    } 
    cout << endl; 
} 
void vLine(int spaces, int rows) 
{       
    int numrows; 
    for (numrows = 0; numrows < rows; numrows++) 
    { 
     row(spaces); 
    } 
} 
void row(int spaces) 
{ 
    int numspaces; 
    cout << "{"; 

    for (numspaces = 0; numspaces < spaces*2+3; numspaces++) 
    {  
     cout << "$"; 
    } 

    cout << "}" << endl; 
} 

void dimensions(int& width, int& height) 
{ 
    cout<<"Enter the width of the house"<<endl; 
    cin>>width; 
    cout<<"Enter the height of the house"<<endl; 
    cin>>height; 
}