diff options
Diffstat (limited to 'CPP/fancy_greeting.cpp')
| -rw-r--r-- | CPP/fancy_greeting.cpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/CPP/fancy_greeting.cpp b/CPP/fancy_greeting.cpp new file mode 100644 index 0000000..ea1aa4c --- /dev/null +++ b/CPP/fancy_greeting.cpp @@ -0,0 +1,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; +} |
