fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <functional>
  4. #include <iterator>
  5. using namespace std;
  6.  
  7. struct Cat{
  8. size_t weight, height;
  9. };
  10.  
  11. int main() {
  12. vector<Cat> cats;
  13. char choice;
  14. bool quit = false;
  15. function<void()> actions[] = {
  16. [&cats]{
  17. Cat cat;
  18. cin >> cat.weight >> cat.height;
  19. cats.push_back(cat);
  20. },
  21. [&cats]{
  22. for(const auto &cat : cats)
  23. cout << "Weight: " << cat.weight << " "
  24. << "Height: " << cat.height << endl;
  25. },
  26. [&quit]{
  27. quit = true;
  28. }
  29. };
  30. while(cin>>choice && !quit){
  31. auto translatedChoice = choice-'0'-1;
  32. if(choice > '0' && translatedChoice<distance(begin(actions), end(actions)))
  33. actions[translatedChoice]();
  34. else cout << "No action associated to the [" << choice << "] index" << endl;
  35. }
  36. return 0;
  37. }
Success #stdin #stdout 0s 3236KB
stdin
1 10 15
1 5 6
2
6
4
3
stdout
Weight: 10 Height: 15
Weight: 5 Height: 6
No action associated to the [6] index
No action associated to the [4] index