Создание трапеции с использованием символа, введенного пользователем. (Консольное приложение) - PullRequest
0 голосов
/ 04 марта 2010

Я пытаюсь создать трапецию, используя введенные пользователем параметры. Я знаю, что мой код может быть не лучшим способом, но пока он работает! Моя проблема в том, что мне нужно, чтобы основание трапеции касалось левой части окна вывода. Что я делаю не так?

#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 Ответ

1 голос
/ 04 марта 2010

setw устанавливает ширину для следующей операции, а не всю строку.Таким образом, ширина одиночного cout << fill установлена ​​в значение.Это дает вам заполнение, но вам нужно установить setw в 0 для последней строки. </p>

также, кажется, есть некоторая избыточная попытка кода:

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