开发者

Creating a Trapezoid using a character inputted by the user. (Console App)

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜