我必须创建一个程序,询问行数和列数,并根据这些值打印一个矩形。 我得到的唯一线索是:
请注意,
char tkn
可用于声明字符。
我收到了输入和输出应该如何显示的示例:
The number of lines: 3
The number of columns: 4
Which characters do you want to use: #
####
####
####
这是我到目前为止所得到的(我现在只是猜测如何做char
位):
#include <iostream>
using namespace std;
int main()
{
int lines, columns, character;
char tkn;
cout<<"The number of lines: ";
cin>>lines;
cout<<"The number of columns ";
cin>>columns;
cout<<"What character do you want to use? ";
cin>>tkn;
cin.ignore();
getchar();
return 0;
}
答案 0 :(得分:0)
添加标题
#include <iomanip>
并包含以下循环
std::cout << std::setfill( tkn );
while ( lines-- )
{
std::cout << std::setw( columns + 1 ) << '\n';
}
最好使用标识符character
(或简称c
)或filler
代替此奇怪标识符tkn
答案 1 :(得分:0)
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int lines, columns, character, i=0, l=0;
char tkn;
cout<<"The number of lines: ";
cin>>lines;
cout<<"The number of columns ";
cin>>columns;
cout<<"What character do you want to use? ";
cin>>tkn;
for(l=0;l<lines;l++;)
{
{
cout<<tkn;
}
for(i=0;i<columns;i++)
{
cout<<tkn;
}
cout<<endl;
}
cin.ignore();
getchar();
return 0;
}