2014-12-15 5 views
-9

사용자의 입력을 읽는 프로그램의 함수로 작업하고 있습니다. 함수는 함수가 존재하는지 확인한 다음 실행하지만 매우 버그가 있습니다. 함수는 실행되도록 설계되어 두 번 실행됩니다.함수가 입력을 읽고 함수를 두 번 실행합니다.

#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; 
} 

는 "기능"라는 헤더 파일을 만들고이 붙여 :

#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; 
} 
+0

코드가 불완전합니다. ** 게시물 **을 수정하고 [MVCE] (http://stackoverflow.com/help/mcve)를 포함하십시오. –

+2

**'i = i ++'**하지 마라. 그것은'i ++'이어야합니다. – 0x499602D2

답변

0

이 있습니다 소스에서

: 여기

  //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](); 
    } 
} 
} 

은 실행하기위한 버전입니다 문제를 일으킬 수있는 코드 문제가 몇 개 있습니다. 첫 번째는 함수 맵을 통해 반복되고 명령이 존재하면 호출하는 것입니다. 문제는 매 반복마다 동일한 명령을 확인하므로 맵에 둘 이상의 요소가 포함되어 있으면 각 명령에 대해 명령이 호출된다는 것입니다. 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](); 
    } 
} 
+0

고마워!, 지금은 잘됐다. 나는지도와 그 기능에 익숙하지 않아서 '.find()'에 대해 잘 알지 못했다. – ezra

관련 문제