2014-04-12 2 views
0

영숫자 값만 처리하려는 방식으로 프로그램을 작동시키고 있지만 마침표, 하이픈 및 밑줄도 허용하는 예외를 만들려고합니다. 그러나 나는 다른 모든 문자를 불법으로 무효화하고 싶다.영숫자가 아닌 문자를 추가하는 방법

void AccountData::assignAccount() 
{ 
    std::cout << "Input Account Name: "; 
    std::string inputAccount; 
    std::getline(std::cin, inputAccount); 
    std::string useAccount = inputAccount.substr(0, 15); 

    if (std::all_of(begin(useAccount), end(useAccount), std::isalnum)) varAccount = useAccount; 
    else 
    { 
     bool valid = true; 

     while (valid) 
     { 
      std::cout << "\nAccounts can only contain alphanumeric values with exceptions of _-.\n\nInput Account Name: "; 
      std::getline(std::cin, inputAccount); 
      useAccount = inputAccount.substr(0, 15); 

      if (std::all_of(begin(useAccount), end(useAccount), std::isalnum)) 
      { 
       varAccount = useAccount; 
       valid = false; 
      } 
     } 
    } 
} 

답변

2

당신은 당신의 자신의 술어를 작성하고이 같은 all_of와 함께 사용할 수 있습니다

bool myFun(char a) 
{ 
    return (isalnum(a) || a=='_' || a=='-' || a=='.'); 
} 
void AccountData::assignAccount() 
{ 
    std::cout << "Input Account Name: "; 
    std::string inputAccount; 
    std::getline(std::cin, inputAccount); 
    std::string useAccount = inputAccount.substr(0, 15); 

    if (std::all_of(begin(useAccount), end(useAccount), myFun)) varAccount = useAccount; 
    else 
    { 
     bool valid = true; 

     while (valid) 
     { 
      std::cout << "\nAccounts can only contain alphanumeric values with exceptions of _-.\n\nInput Account Name: "; 
      std::getline(std::cin, inputAccount); 
      useAccount = inputAccount.substr(0, 15); 

      if (std::all_of(begin(useAccount), end(useAccount), myFun)) 
      { 
       varAccount = useAccount; 
       valid = false; 
      } 
     } 
    } 
} 
+0

을 내가 필요 정확히 무엇인지, 매우 통찰력, 감사합니다! –

관련 문제