使用用户输入的字符创建梯形。 (控制台应用程序)
我正在尝试使用用户输入的选项创建一个梯形。我知道我的代码可能不是最好的方法,但到目前为止它有效!我的问题是我需要梯形的底边接触输出窗口的左侧。我做错了什么?
#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;
}
}
I am trying to create a trapezoid using user inputted options. I know my code may not be the best way but so far it works! My problem is i need the base of the trapezoid to be touching the left side of the output window. What am i doing wrong?
#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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setw 设置下一个操作的宽度,而不是整行。所以,单个cout的宽度<< fill 设置为该值。这为您提供了填充,但您需要将最后一行的 setw 设置为 0。
另外,似乎有一些多余的代码尝试:
setw sets the width for the next operation, not the entire line. So, the width of a single cout << fill is set to the value. This is giving you the padding, but you need to set setw to 0 for the final row.
also, there seems to be some redundant code try: