Advertisement
Temabowl

C++ akinator

Dec 13th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <filesystem>
  5.  
  6. #include "nlohmann/json.hpp"
  7.  
  8. int main() {
  9. // Default file content: {"This are big":["Whale", "Cat"]}
  10. std::ifstream input("D://input.txt");
  11.  
  12. nlohmann::json j;
  13. input >> j;
  14. input.close();
  15.  
  16. auto question = j.begin();
  17. char resp;
  18. while (true) {
  19. std::cout << question.key() << "? (y/n)" << std::endl;
  20. std::cin >> resp;
  21.  
  22. auto& answer = question.value()[resp == 'y' ? 0 : 1];
  23. if (answer.is_object()) {
  24. question = answer.begin();
  25. continue;
  26. }
  27. else {
  28. std::cout << answer.get<std::string>() << std::endl
  29. << "That's right? (y/n)" << std::endl;
  30. std::cin >> resp;
  31. if (resp == 'y') {
  32. std::cout << "Cool!" << std::endl;
  33. return 0;
  34. }
  35. else {
  36. std::cin.ignore();
  37. std::string a, b, c = answer.get<std::string>();
  38. std::cout << "What did you think?" << std::endl;
  39. std::getline(std::cin, a);
  40.  
  41. std::cout << "How does it differ from " << c << '?' << std::endl;
  42. std::getline(std::cin, b);
  43.  
  44. std::cout << "OK, I'll remember this!" << std::endl;
  45.  
  46. answer = { {b, {a, c}} };
  47. }
  48. break;
  49. }
  50. }
  51.  
  52. std::ofstream output("D://input.txt.tmp");
  53. output << j;
  54. output.close();
  55. std::filesystem::rename("D://input.txt.tmp", "D://input.txt");
  56.  
  57. system("pause");
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement