2015-01-31 3 views
0

나는리스트에서 3 명의 플레이어를 선택하도록하는 매우 간단한 프로그램을 가지고있다. 사용자가 동일한 플레이어를 두 번 선택하지 못하게 할 방법이 있습니까? 원래는 사용자가 선택하는 항목이 std :: set에 추가 될 것이고 그 다음에 해당 항목을 세트와 비교하여 검사 할 것입니다. 그렇다면 그 선택은 항상 세트에 추가되기 때문에 그 아이디어가 얼마나 우스운 지 깨달았습니다.사용자가 동일한 선택을 두 번하고 있는지 확인하는 방법은 무엇입니까?

#include <iostream> 
    #include <string> 

int main() 
{ 
int m_NumOfPlayers; 

std::string m_PlayerSelection [6] = 
{ 
    "Miss Scarlet", 
    "Mrs. Peacock", 
    "Colonel Mustard", 
    "Professor Plum", 
    "Mrs. White", 
    "Mr. Green" 
}; 

std::string m_Number [6] = 
{ 
    "first", 
    "second", 
    "third", 
    "fourth", 
    "fifth", 
    "sixth" 
}; 

for (int i = 0; i < 3; i++) 
{ 
    std::cout << "\nPlease choose the " << m_Number[i] << " player" << ":" << std::endl << std::endl; 
    for (int j = 0; j < 6; j++) 
    { 
     std::cout << (j + 1) << ". " << m_PlayerSelection[j] << std::endl; 
    } 

    int selection; 
    std::cin >> selection; 

    while (selection < 1 || selection > 6) 
    { 
     std::cout << "Please choose a number between 1 and 6: "; 
     std::cin >> selection; 
    } 
} 
system("pause"); 
return 0; 
} 
+0

귀하의 제안 작동? 세트가 더 적절한 구조 일 것이다. 하나는 빠른 포함 기능입니다. 어쩌면 해시 테이블. 보다 사용자 친화적 인 접근 방법은 선택할 수없는 항목을 숨기거나 제거하는 것입니다. – keyser

답변

0

이 사용할 수 있습니다 : 당신이 바로, 중복을 피하고, 또 다른 하나를 추가하기 전에 배열을 선택하면

#include <iostream> 
#include <string> 
#include <iomanip> 

int main() 
{ 
    int m_NumOfPlayers; 

    std::string m_PlayerSelection [6] = 
    { 
     "Miss Scarlet", 
     "Mrs. Peacock", 
     "Colonel Mustard", 
     "Professor Plum", 
     "Mrs. White", 
     "Mr. Green" 
    }; 

    std::string m_Number [6] = 
    { 
     "first", 
     "second", 
     "third", 
     "fourth", 
     "fifth", 
     "sixth" 
    }; 

    int taken[7]={0,0,0,0,0,0,0};// Array to keep track of the selected names 

    for (int i = 0; i < 3; i++) 
    { 
     std::cout << "\nPlease choose the " << m_Number[i] << " player" << ":" << std::endl << std::endl; 
     for (int j = 0; j < 6; j++) 
     { 
      std::cout << (j + 1) << ". " << m_PlayerSelection[j] ; 

      // printing if the name is already taken by someone 
      if(taken[j+1] == 1) 
      { 
       std::cout << std::setw(10); 
       std::cout << "-- taken" << std::endl; 
      } 
      else 
       std::cout << std::endl; 
     } 

     int selection; 
     std::cin >> selection; 

     while (selection < 1 || selection > 6 || taken[selection]) 
     { 
      std::cout << "Please choose a number between 1 and 6 which is not taken already: "; 
      std::cin >> selection; 
     } 
     taken[selection]=1; // mark selected name 
    } 
    return 0; 
} 
0

아마도 많은 방법이있을 수 있습니다.하지만 저는 여전히 새로운 것이므로 데이터 구조를 벡터로 변경하는 것입니다. 그렇게하면 물건을 밀어 낼 수 있습니다.

#include <vector> //Necessary if you are not familiar with vectors. 
#include <iostream> 
using namespace std; 
int main(){ 
    vector<string> names; 
    names.push_back("Miss Scarlet"); 
    names.push_back("Mrs. Peacock"); 
    //...Keep going 
    for(int i=0; i<3; i++){ 
     string temp; 
     cout<<"Select"; 
     cin>>temp; 
     names.erase(find(vector.begin(), vector.end(), temp)!=vector.end()); 
    } 
} 

나는 마법사가 아니지만 잘하면 충분합니다.

관련 문제