为姓名装框,c++初学者必会

c++课堂练习,为姓名装框,打印名片类似问题。

一下我分享两种方法:

第一种:一行一行的打印。

#include <iostream>
#include<string>

int main()
{
	using namespace std;
	
	cout << "please inter your name:"<<endl;
	string name;
	cin >> name;
	const string greeting = "hello," + name + "!";
	const string spaces(greeting.size(),' ');
	const string second = "*" + spaces + "*";
	const string first(second.size(), '*');
	cout << endl;
    //一行一行打印
	cout << first << endl;
	cout << second << endl;
	cout << "*" << greeting << "*" << endl;
	cout << second << endl;
	cout << first << endl;
	return 0; 
}

第二种:

分模块打印,分别为打印“*”号方框,打印空格,打印“hello,姓名!”

#include<iostream>
#include<string>

using namespace std;
int main()
{
cout << "please inter your name :" << endl;
	string name;
	cin >> name;
	const string greeting = "hello," + name + "!";
	const int pad = 1;
	const int rows = pad * 2 + 3;//行数
	const string::size_type cols = greeting.size() + pad * 2 + 2;//列数
	cout << endl;
	for (int r = 0; r != rows; r++)
	{
		string::size_type c = 0;
		while (c != cols)
		{
			if (r == pad + 1 && c == pad + 1)
			{
				cout << greeting+" ";

				c += greeting.size();

			}
			else
			{
				if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1)
				{
					cout << "*";

				}
				else
				{
					cout << " ";
				}
			}
			c++;
		}
		cout << endl;
		
	}

	return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

森林猿go

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值