zoukankan      html  css  js  c++  java
  • Using Input String to Call Function

    #include <iostream>
    #include 
    <string>
    using namespace std;

    void male()
      {
      cout 
    << "I'm a man.\n";
      }

    void female()
      {
      cout 
    << "I'm a woman.\n";
      }

    struct
      {
      (
    void)(*fn)();
      
    const char* key;
      }
    function_lookup_table[] 
    =
      {
      { 
    &male,   "male"   },
      { 
    &female, "female" },
      { NULL,    NULL     }
      };

    bool lookup_and_call( const string& name )
      {
      
    for (int i = 0; function_lookup_table[ i ].fn; i++)
        
    if (name == function_lookup_table[ i ].key)
          {
          (
    *(function_lookup_table[ i ].fn))();
          
    return true;
          }
      
    return false;
      }

    int main()
      {
      
    string users_gender;
      cout 
    << "Enter male or female> " << flush;
      getline( cin, users_gender );
      
    if (!lookup_and_call( users_gender ))
        {
        cout 
    << "What?\n";
        }
      
    return 0;
      } 
  • 相关阅读:
    Beta冲刺(4/4)
    2019 SDN上机第7次作业
    Beta冲刺(3/4)
    Beta冲刺(2/4)
    机器学习第二次作业
    机器学习第一次作业
    软工实践个人总结
    第04组 Beta版本演示
    第04组 Beta冲刺(5/5)
    第04组 Beta冲刺(4/5)
  • 原文地址:https://www.cnblogs.com/smartvessel/p/2015994.html
Copyright © 2011-2022 走看看