使用用户输入的字符创建梯形。 (控制台应用程序)

发布于 2024-08-23 22:50:14 字数 999 浏览 1 评论 0原文

我正在尝试使用用户输入的选项创建一个梯形。我知道我的代码可能不是最好的方法,但到目前为止它有效!我的问题是我需要梯形的底边接触输出窗口的左侧。我做错了什么?

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨的味道风的声音 2024-08-30 22:50:14

setw 设置下一个操作的宽度,而不是整行。所以,单个cout的宽度<< 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; 
}
}

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:

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; 
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文