2010-03-04 42 views
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; 
    } 
} 
+0

这是功课? – Xorlev 2010-03-04 06:39:59

+0

“最好的梯形”是什么意思? – 2010-03-04 06:56:59

回答

1

setw设置下一个操作的宽度,而不是整条线。因此,单个cout的宽度填充设置为该值。这是给你的填充,但你需要为最后一行设置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; 
} 
} 
相关问题