Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. const std::string name = "Bob";
  7.  
  8. bool isUpper(const std::string& input)
  9. {
  10.     std::string str = input;
  11.  
  12.     str.erase(std::remove_if(str.begin(), str.end(), [](const char& c)
  13.         {
  14.             return !isalpha(c) || c == ' ';
  15.         }), str.end());
  16.  
  17.     return std::all_of(str.begin(), str.end(), [](const char& c) {return std::isupper(c); });
  18. }
  19.  
  20. size_t getResponse(const std::string& input)
  21. {
  22.     size_t response = 5;
  23.  
  24.     if (isUpper(input))
  25.     {
  26.         if (input.find("?") != std::string::npos)
  27.             response = 3;
  28.         else
  29.             response = 2;
  30.     }
  31.     else if (input.find("?") != std::string::npos)
  32.         response = 1;    
  33.     else if (input == name)
  34.         response = 4;
  35.  
  36.     return response;
  37. }
  38.  
  39. int main()
  40. {    
  41.     /*
  42.         1 = question
  43.         2 = yell
  44.         3 = yell a question
  45.         4 = mention
  46.         5 = anything else
  47.     */
  48.  
  49.     std::map<size_t, std::string> answers = { {1,"Sure."}, {2,"Whoa, chill out!"}, {3,"Calm down, I know what I'm doing!"},{4,"Fine. Be that way!"}, {5,"Whatever."} };
  50.     std::map<size_t, std::string>::const_iterator it;
  51.     std::string input;
  52.     std::cout << "\t********** Talk with Bob... **********" << std::endl;
  53.     do
  54.     {  
  55.         std::cout << "You: ";
  56.         std::getline(std::cin, input);
  57.  
  58.         if (input != "exit" && input != " ")
  59.         {
  60.             auto response = getResponse(input);
  61.  
  62.             it = answers.find(response);
  63.  
  64.             if (it != answers.end())
  65.             {
  66.                 std::cout << name << ": " << it->second << std::endl;
  67.             }
  68.         }        
  69.  
  70.     } while (input != "exit");
  71.  
  72.     __debugbreak();
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement