大家好, 小弟係一個學緊C++既新手, 係寫一個小程式當中出現左小小問題, 希望各位師兄指教
呢個係我想要既效果,
This program will output a parallelogram.
How long do you want each side to be? 5
Please enter the character you want it to be made of: #
#
##
###
####
#####
####
###
##
#
但係我寫出黎既效果就係咁
How long do you want each side to be?5
Please enter the character you want it to be made of:#
#
##
###
####
#####
# # # #
# # #
# #
#
我諗係我用SETFILL同SETW出左問題, 但係我試左好耐都搞唔掂, 希望各位指點指點!, 謝謝 (仲有係我既LOOP 寫得有冇問題? 有冇野要改進?)
呢度係我寫既CODE...
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
int r;
int c;
char w;
cout << "How long do you want each side to be?" ;
cin >> n;
cout << "Please enter the character you want it to be made of:" ;
cin >> w;
for (r = 1; r <= n; ++r)
{
for (c = 1; c <= r; ++c)
cout << w;
cout << endl;
}
for (r = n - 1; r >= 1; --r)
{
for (c = 1; c <= r; ++c)
cout << setw(n-r+1) << right << w;
cout << endl;
}
} |