fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <limits>
  4.  
  5. void simple_name(std::string& first_name, std::string& last_name);
  6. void complex_name(std::string& first_name, std::string& last_name);
  7.  
  8. int main() {
  9. std::string first, last;
  10.  
  11. std::cout << "Simple method.\n";
  12. simple_name(first, last);
  13.  
  14. std::cout << "\nYour name (simple method): " << first << " " << last << std::endl;
  15.  
  16. std::cout << "\nComplex method.\n";
  17. complex_name(first, last);
  18.  
  19. std::cout << "\nYour name (complex method): " << first << " " << last << std::endl;
  20. }
  21.  
  22. void simple_name(std::string& first_name, std::string& last_name) {
  23. char first_operator, second_operator;
  24. std::cout << "Please enter the first seperation operator :";
  25. std::cin >> first_operator; // Get first character from input.
  26. std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Ignore the rest.
  27. std::cout << "Please enter the second seperation operator :";
  28. std::cin >> second_operator; // Get first character from input.
  29. std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Ignore the rest.
  30.  
  31. std::cout << "Please enter your name:";
  32. std::getline(std::cin, first_name, first_operator );
  33.  
  34. std::getline(std::cin, last_name, second_operator );
  35.  
  36. std::cin.ignore();
  37. }
  38.  
  39. void complex_name(std::string& first_name, std::string& last_name) {
  40. std::string first_operator, second_operator;
  41. std::cout << "Please enter the first seperation operator :";
  42. std::getline(std::cin, first_operator);
  43. std::cout << "Please enter the second seperation operator :";
  44. std::getline(std::cin, second_operator);
  45.  
  46. bool done = false;
  47.  
  48. do {
  49. std::string name_buf;
  50. std::cout << "Please enter your name:";
  51. std::getline(std::cin, name_buf);
  52.  
  53. auto first_op = name_buf.find(first_operator);
  54. auto second_op = name_buf.find(second_operator);
  55. if (first_op == std::string::npos || second_op == std::string::npos) {
  56. std::cout << "Please separate your first and last names with the first specified operator, "
  57. << "and follow your last name with the second specified operator.\n";
  58. continue;
  59. }
  60.  
  61. first_name = name_buf.substr(0, first_op);
  62. auto last_start = first_op + first_operator.size();
  63. last_name = name_buf.substr(last_start, second_op - last_start);
  64. done = true;
  65. } while(!done);
  66. }
Success #stdin #stdout 0s 3476KB
stdin
|
+
Robert');|DROP TABLE Students;--+
||==||
==||==
Robert');||==||DROP TABLE Students;--==||==
stdout
Simple method.
Please enter the first seperation operator :Please enter the second seperation operator :Please enter your name:
Your name (simple method): Robert'); DROP TABLE Students;--

Complex method.
Please enter the first seperation operator :Please enter the second seperation operator :Please enter your name:
Your name (complex method): Robert'); DROP TABLE Students;--