函数读取输入并运行两次函数

时间:2014-12-15 02:48:16

标签: c++ for-loop map

我正在一个程序中的函数来读取用户的输入然后,该函数将检查该函数是否存在然后运行它,但它是非常错误的并且函数意味着要运行,运行两次。

            //functions with a int and a string
     std::map<std::string, std::function<void(int, string)>> functionsIS = { 
             {"printWordWithNumber", numberPlusWord}, 
     };
     //functions with no parameters
     std::map<std::string, std::function<void()>> functionsNI = {
             {"Help", userHelp},
     }; 
void CommandCheck(std::string command){
int paramInt;
string paramString;
for (int i = 0; i < functionsIS.size(); i = i++){
    if (functionsIS[command]){
        std::cout << "Accessed '" << command << "' reading requirements..." << std::endl;
        std::cout << "Enter paramater one (integer) : ";
        std::cin >> paramInt;
        std::cout << std::endl<<"Enter paramater two (string)" << std::endl;
        std::cin.ignore();
        std::getline(std::cin,paramString);
        std::cout << "running..." << std::endl;
        functionsIS[command](paramInt,paramString);
    }
}
for (int i = 0; i < functionsNI.size(); i = i++){
    if (functionsNI[command]){
        std::cout << "Accessed '" << command << "' running..." << std::endl;
        functionsNI[command]();
    }
}
}

以下是您要运行的版本:

来源:

#include <iostream>
#include <map>
#include <windows.h>
#include <string>
#include <vector>
#include <map>
#include <functional>

#include "userFunctions.h"//header file for functions

using namespace std;

std::string input;
//functions with a int and a string
std::map<std::string, std::function<void(int, string)>> functionsIS = {
    { "printWordWithNumber", numberPlusWord },
};
//functions with no parameters
std::map<std::string, std::function<void()>> functionsNI = {
    { "Help", userHelp },
};

void CommandCheck(std::string command){
int paramInt;
string paramString;
for (int i = 0; i < functionsIS.size(); i = i++){
    if (functionsIS[command]){
        std::cout << "Accessed '" << command << "' reading requirements..." << std::endl;
        std::cout << "Enter paramater one (integer) : ";
        std::cin >> paramInt;
        std::cout << std::endl << "Enter paramater two (string)" << std::endl;
        std::cin.ignore();
        std::getline(std::cin, paramString);
        std::cout << "running..." << std::endl;
        functionsIS[command](paramInt, paramString);
    }
}
for (int i = 0; i < functionsNI.size(); i = i++){
    if (functionsNI[command]){
        std::cout << "Accessed '" << command << "' running..." << std::endl;
        functionsNI[command]();
    }
}
}

int main(){
do{
    std::cout << "Waiting For Command..." << std::endl;
    cin >> input;
    CommandCheck(input);
} while (input != "end");


return 0;
}

创建一个名为“functions”的头文件并粘贴它:

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

void numberPlusWord(int number, std::string word){
std::cout << word << std::endl;
std::cout << number << std::endl;
}

void userHelp(){
std::cout << "I can help!" << std::endl;
}

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题会导致问题。第一个是你通过函数映射迭代,如果命令存在,你正在调用它。问题是您在每次迭代时检查相同的命令,因此如果映射包含多个元素,则将为每个元素调用该命令。您可以通过删除for循环并使用映射的find函数来确定命令是否存在来解决此问题。

第二个问题是,如果命令不存在,则会在地图中为其创建一个元素。下面的if语句将使用command作为密钥自动插入元素。

 if(functionsIS[command]) { /*...*/}

以下对代码的更新将解决问题。

void CommandCheck(std::string command)
{
    int paramInt;
    string paramString;

    if (functionsIS.find(command) != functionsIS.end())
    {
        std::cout << "Accessed '" << command << "' reading requirements..." << std::endl;
        std::cout << "Enter paramater one (integer) : ";
        std::cin >> paramInt;
        std::cout << std::endl << "Enter paramater two (string)" << std::endl;
        std::cin.ignore();
        std::getline(std::cin, paramString);
        std::cout << "running..." << std::endl;
        functionsIS[command](paramInt, paramString);
    }
    else if (functionsNI.find(command) != functionsNI.end())
    {
        std::cout << "Accessed '" << command << "' running..." << std::endl;
        functionsNI[command]();
    }
}