1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include <functional>
#include <vector>
class Actions{
public:
std::string open(){
return "You opened something...\n";
}
std::string close(){
return "You closed something...\n";
}
std::string pickup(){
return "You picked something up...\n";
}
std::string putdown(){
return "You put something down...\n";
}
}myActions;
bool prompt(int &choice)
{
std::cout<<"Function: 1, 2, 3, 4, \nor 5 to Exit\nChoice: ";
std::cin>>choice;
std::cin.clear();
std::cin.ignore(1000,'\n');
return true;
}
int main()
{
int choice;
std::function<std::string()> *Fun;
std::vector<std::function<std::string()>> Commands =
{
std::bind(&Actions::open, myActions),
std::bind(&Actions::close, myActions),
std::bind(&Actions::pickup, myActions),
std::bind(&Actions::putdown, myActions)
};
while(true){
while(prompt(choice)&&(choice<1||choice>Commands.size()+1))
std::cout<<"Invalid choice...\n\n";
if(choice==Commands.size()+1)
break;
Fun=&Commands[choice-1];
std::cout<<(*Fun)()<<'\n';
}
return 0;
}
|
Function: 1, 2, 3, 4,
or 5 to Exit
Choice: 1
You opened something...
Function: 1, 2, 3, 4,
or 5 to Exit
Choice: 2
You closed something...
Function: 1, 2, 3, 4,
or 5 to Exit
Choice: 3
You picked something up...
Function: 1, 2, 3, 4,
or 5 to Exit
Choice: 4
You put something down...
Function: 1, 2, 3, 4,
or 5 to Exit
Choice: 5
Process returned 0 (0x0) execution time : 9.417 s
Press any key to continue. |