Text Frame

I'm transitioning from python to C++ and am doing the classic text frame problem.
I need to surround a welcome sign in "*". The text dose not have to be centered.
My idea was to insert a blank space in the string until it was the length of the max length of either welcome string,first name string, or last name string. Unfortunately in max() only takes two parameters. I'm also having trouble comparing the length of the string to the max length, as one is a string and the other is a size_t. Any help would be great!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string>
#include <algorithm>

void greetings(string first, string last)
{
	string star = "*";
	string greet = "** Hello, **";
	string f = "** " + first + " **";
	string l = "** " + last + " **";
	size_t n = max(greet.length(),f.length(),l.length());
	
	
	while (greet.length() < n)
	{
		greet.insert(11, " ");
	}
	while (f.length() < n)
	{
		f.insert(f.length() + 3, " ");
	}
	while (l.length() < n)
	{
		l.insert(l.length() + 3, " ");
	}
	while (star.length() < n);
	{
		star += "*";
	}
	cout << star << "\n";
	cout << greet << "\n";
	cout << f << "\n";
	cout << l << "\n";
	cout << star << "\n";
}

int main()
{
        string fn;
	string ln;
	cout << "Enter your first name: ";
	cin >> fn;
	cout << "Enter your last name: ";
	cin >> ln;
	greetings(fn, ln);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void greetings(string first, string last)
{
	string star = "*";
	string greet = "** Hello, **";
	string f = "** " + first + " **";
	string l = "** " + last + " **";
	unsigned int a=greet.length(),b=f.length(),c=l.length();
	size_t n=max(a,b);
    n=max(n,c);
	while (greet.length() < n)
	{
		greet.insert(11, " ");
	}
	while (f.length() < n)
	{
		f.insert(f.length() + 3, " ");
	}
	while (l.length() < n)
	{
		l.insert(l.length() + 3, " ");
	}
	while (star.length() < n);
	{
		star += "*";
	}
	cout << star << "\n";
	cout << greet << "\n";
	cout << f << "\n";
	cout << l << "\n";
	cout << star << "\n";
}

int main()
{
    string fn;
	string ln;
	cout << "Enter your first name: ";
	cin >> fn;
	cout << "Enter your last name: ";
	cin >> ln;
	greetings(fn, ln);
}
Topic archived. No new replies allowed.