新的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
函數的答案。感謝您的時間!
是什麼通過與調試代碼加強告訴你的原因造成的問題? –
當你調用getDimensions時,你的寬和高的順序是錯誤的,所以這些值會被翻轉。 – cambunctious
另一個問題是,你認爲屋頂的底部是高度的兩倍,而對正確輸出的一小部分可以看出它不是。你已經知道屋頂應該有多寬 - 高度不得而知。 (但你不需要計算它的高度。) – molbdnilo