fork(2) download
  1. // ask for a person's name, and greet the person
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using std::cout;
  6. using std::cin;
  7. using std::string;
  8.  
  9. int main()
  10. {
  11. // fetch name
  12. cout << "Please enter your first name: ";
  13. string name;
  14. cin >> name;
  15.  
  16. // message
  17. const string greeting = "Hello, " + name + "!";
  18. // padding
  19. const int pad = 1;
  20. //desired rows/columns
  21. const int rows = pad * 2 + 3;
  22. const string::size_type cols = greeting.size() + pad * 2 + 2;
  23. // seperate output from input
  24. cout << std::endl;
  25. // invariants
  26. int r = 0;
  27.  
  28.  
  29. while (r != rows) {
  30. string::size_type c = 0;
  31. while(c != cols) {
  32. if (r == 0 || r == rows -1 || c == 0 || c == cols -1) { // if in bordering column or row
  33. cout << "*"; //output *
  34. } else {
  35. if (r == pad + 1 && c == pad + 1) { //if on row for greeting
  36. cout << greeting; // write greeting
  37. c += (greeting.size()-1); // adjust invariant
  38. } else {
  39. cout << " ";
  40. }
  41. }
  42. ++c;
  43. }
  44. ++r;
  45. cout << std::endl;
  46. }
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 3436KB
stdin
Johnny
stdout
Please enter your first name: 
******************
*                *
* Hello, Johnny! *
*                *
******************