初学者 c++ 画房子程序

beginner c++ draw house program

C++ 的新手所以如果我有任何错误,请不要嘲笑我。我正在尝试编写一个程序,该程序将向用户询问单位,然后使用函数绘制房子。我遇到的问题出在我的 drawcone 函数中 这是我目前的进展。

#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 函数的答案。感谢您的宝贵时间!

应该很容易调试,它打印出房子 'roof' 的错误空格数。 该问题应包含在 while ( r != height) 块中 尝试用 else if(c%2==1){cout << " ";}

弄乱 else {cout << " ";}

这是一种解决方案。但是,只有当宽度为奇数时,锥体才会对称。

#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;

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