views:

51

answers:

1

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;
    }
}
+1  A: 

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