我正在尝试使用用户输入的选项创建一个梯形。我知道我的代码可能不是最好的方法,但到目前为止它的工作原理!我的问题是我需要梯形的底部触摸输出窗口的左侧。我做错了什么?
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int topw, height, width, rowCount = 0, temp;
char fill;
cout << "Please type in the top width: ";
cin >> topw;
cout << "Please type in the height: ";
cin >> height;
cout << "Please type in the character: ";
cin >> fill;
width = topw + (2 * (height - 1));
cout<<setw(width);
for(int i = 0; i < topw;i++)
{
cout << fill;
}
cout << endl;
rowCount++;
width--;
temp = topw + 1;
while(rowCount < height)
{
cout<<setw(width);
for(int i = 0; i <= temp; i++)
{
cout << fill;
}
cout << endl;
rowCount++;
width--;
temp = temp +2;
}
}
答案 0 :(得分:1)
setw设置下一个操作的宽度,而不是整行。因此,单个cout的宽度<&lt;&lt; fill设置为值。这将为您提供填充,但您需要将setw设置为0以用于最后一行。
另外,似乎有一些冗余代码尝试:
int main()
{
int topw, height, width, rowCount = 0, temp;
char fill;
cout << "Please type in the top width: ";
cin >> topw;
cout << "Please type in the height: ";
cin >> height;
cout << "Please type in the character: ";
cin >> fill;
width = height;
cout<<setw(width);
temp = topw;
while(rowCount < height)
{
cout<<setw(width);
for(int i = 0; i < temp; i++)
{
cout << fill;
}
cout << endl;
rowCount++;
width--;
temp = temp +2;
}
}