summaryrefslogtreecommitdiff
path: root/CPP/fancy_greeting.cpp
blob: ea1aa4ceb09d05ccbfb2e713ac50b9f890600364 (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <string>

int main(void) {

	std::string name;

	std::cout << "Please enter your first name: ";
	std::cin >> name;

	// Build the welcome msg
	const std::string greeting = "Hello, " + name + "!";

	// blank rows
	const int pad = 1;

	// total number of rows to write 1 for greetings 2 for top and bottom
	const int rows = pad * 2 + 3;
	const std::string::size_type cols = greeting.size() + pad * 2 + 2;

	// Separate output from input
	std::cout << std::endl;

	for (int r = 0; r != rows; r++) {

		// size_type, defined by string class, defines the name of the
		// appropriate type for holding the number of chars in a string.
		std::string::size_type c = 0;

		while (c != cols) {
			if (r == pad + 1 && c == pad + 1) {
				std::cout << greeting;
				c += greeting.size();
			} else {
				if (r == 0 || r == rows - 1 ||
				    c == 0 || c == cols - 1)
					std::cout << "*";
				else
					std::cout << " ";
				c++;
			}
			// write one or more chars
			// adjust the value of c to maintain the invariant
		}
		// write a row of output
		std::cout << std::endl;
	}

	// build the second and fourth lines of the output
	const std::string spaces(greeting.size(), ' ');
	const std::string second = "* " + spaces + " *";

	// build the first and fifth lines of the output
	const std::string first(second.size(), '*');

	// Write everything
	std::cout << std::endl;
	std::cout << first << std::endl;
	std::cout << second << std::endl;
	std::cout << "* " << greeting << " *" << std::endl;
	std::cout << second << std::endl;
	std::cout << first << std::endl;

	const std::string ex = "!";
	const std::string msg = "hello" + ex;
	std::cout << msg << std::endl;

	{ const std::string s = "a string";
		std::cout << s << std::endl; }
	{ const std::string s = "another string";
		std::cout << s << std::endl; }

	{ const std::string s = "a string";
		std::cout << s << std::endl;
		{ const std::string s = "another string";
			std::cout << s << std::endl; }}

	std::cout << "What is your name? ";
	std::string n;
	std::cin >> n;
	std::cout << "Hello, " << n
		<< std::endl << "And what is yours? ";
	std::cin >> n;
	std::cout << "Hello, " << n
		<< "; nice to meet you too!" << std::endl;

	return 0;
}