2017-11-15 34 views
0

프로그램에서 입력과 관련하여 여러 가지 문제가 있습니다. 내가 입력 사용자에게 프로그램의 우선 내가 _ 이름이 다른 문자열 변수로 선언이getline()을 사용하여 문자열을 프로그램에 입력하면 다음 입력에 오버플로가 발생합니다.

cout << "Please tell me your name." << endl; 
getline(cin, user_name); 
cout << "Hello " << user_name << " and welcome to Fantasy Battle!" << endl; 

으로 그렇게 자신의 이름입니다. 이 제품은 또한 다음과 같은 메시지가 화면에 표시 할 때 아무런 문제가없는 것처럼 보인다 제대로

사용자의 다음 입력이 코드

{ 
cout << "Hello, what would you like to do?" << endl; 
cout << "1. Play" << endl << "2. Exit" << endl; 
cout << "Please enter the number corresponding to your choice from the list 
above." << endl; 
for(;;) 
{ 
    if(cin >> menuChoice) 
    { 
     if(cin.get() == '.') 
     { 
      cin.clear(); 
      cin.ignore(10000, '\n'); 
     } 
     if(menuChoice == 1 || menuChoice == 2) 
      break; 
     else 
     { 
      cout << "You did not enter a valid menu option. Please try 
      again." << endl; 
      cin.clear(); 
      cin.ignore(100000, '\n'); 
     } 
    } 
    else 
    { 
     cout << "You did not enter a valid menu option. Please try again." 
     << endl; 
     cin.clear(); 
     cin.ignore(100000, '\n'); 
    } 
} 
if(menuChoice == 2) 
{ 
    return 2; 
} 
else 
{ 
    //setup a fight code further down 
} 

하나를 통해 첫 번째 실행가 종료됩니다 내가 2를 입력하면에서 오는

main에서 성공적으로 프로그램을 만들거나 1을 입력하면 fight 함수를 통해 실행됩니다. 그러나, 나는 1 싸움을 통해 나가거나 재생 또는 종료 2 입력 무한한 시간을 입력 할 수있는 프로그램을 나갈 것을 요청하는 프로그램을 다시 얻을 경우 프로그램을 종료하지 않습니다. 그게 원인인지 모르겠습니다.

for(;;) 
    { 
     game = menu(); 
     if(game == 2) 
     { 
      break; 
     } 
     else 
     { 
      fight(); 
     } 
    } 
    return 0; 

내 프로그램의 menu() 기능 내의 코드는 다음과 같으며 내 프로그램의 나머지 입력이 들어 있습니다. getline (cin, fighterName)을 사용하여 사용자가 원하는 각 문자의 이름으로 사용할 문자열을 얻습니다. 문제는 묻지 않고 문자의 이름을 저장하기 시작한다는 것입니다.

cout << "How many fighters should be on Team 1?" << endl; 
//Input Validation 
for(;;) 
{ 
    if(cin.get() == '.') 
    { 
     cin.clear(); 
     cin.ignore(100000, '\n'); 
    } 
    if(cin >> team1Size) 
    { 
     if(team1Size <= 0) 
     { 
      cout << "The team must be a size of at least 1 fighter. Please try again." << endl; 
      cin.clear(); 
      cin.ignore(100000, '\n'); 
     } 
     else 
     { 
      break; 
     } 
    } 
    else 
    { 
     cout << "You did not enter a valid number. Please try again." << endl; 
      cin.clear(); 
      cin.ignore(100000, '\n'); 
    } 
} 

cout << "How many fighters should be on Team 2?" << endl; 
//Input Validation 
for(;;) 
{ 
    if(cin.get() == '.') 
    { 
     cin.clear(); 
     cin.ignore(100000, '\n'); 
    } 
    if(cin >> team2Size) 
    { 
     if(team2Size <= 0) 
     { 
      cout << "The team must be a size of at least 1 fighter. Please try again." << endl; 
      cin.clear(); 
      cin.ignore(100000, '\n'); 
     } 
     else 
     { 
      break; 
     } 
    } 
    else 
    { 
     cout << "You did not enter a valid number. Please try again." << endl; 
      cin.clear(); 
      cin.ignore(100000, '\n'); 
    } 
} 

//Set up team 1 
cout << "Begin setup for team 1:" << endl << endl; 
for(int i = 0; i < team1Size; i++) 
{ 
    cout << "Which character type should fighter " << i+1 << " be?" << endl; 
    cout << "1. Barbarian" << endl; 
    cout << "2. BlueMen" << endl; 
    cout << "3. Vampire" << endl; 
    cout << "4. Medusa" << endl;  
    cout << "5. Harry Potter" << endl; 
    cout << "Please enter the number corresponding to your choice from the list above." << endl; 
    for(;;) 
    { 
     if(cin.get() == '.') 
     { 
      cin.clear(); 
      cin.ignore(100000, '\n'); 
     } 
     if(cin >> fighterType) 
     { 
      if(fighterType < 1 || fighterType > 5) 
      { 
       cout << "You did not enter a valid choice. Please try again." << endl; 
       cin.clear(); 
       cin.ignore(100000, '\n'); 
      } 
      else 
       break; 
     } 
     else 
     { 
      cout << "You did not enter a valid choice. Please try again." << endl; 
      cin.clear(); 
      cin.ignore(100000, '\n'); 
     } 
    } 
    //Now that we have the desired type of the fighter we must add a fighter of the correct type to the linked list 
    //representing team 1. We will do so by calling the add function of the linked list 
    cout << "Please enter the name of this fighter." << endl; 
    getline(cin, fighterName); 
    if(fighterType == 1) 
    { 
     team1.addBack("Barbarian", fighterName); 
    } 
    else if(fighterType == 2) 
    { 
     team1.addBack("BlueMen", fighterName); 
    } 
    else if(fighterType == 3) 
    { 
     team1.addBack("Vampire", fighterName); 
    } 
    else if(fighterType == 4) 
    { 
     team1.addBack("Medusa", fighterName); 
    } 
    else 
    { 
     team1.addBack("HarryPotter", fighterName); 
    } 

} 

cout << "Team 1 has been created!" << endl << endl; 

//Set up team 2 
cout << "Begin setup for team 2:" << endl << endl; 
for(int i = 0; i < team2Size; i++) 
{ 
    cout << "Which character type should fighter " << i+1 << " be?" << endl; 
    cout << "1. Barbarian" << endl; 
    cout << "2. BlueMen" << endl; 
    cout << "3. Vampire" << endl; 
    cout << "4. Medusa" << endl;  
    cout << "5. Harry Potter" << endl; 
    cout << "Please enter the number corresponding to your choice from the list above." << endl; 
    for(;;) 
    { 
     if(cin.get() == '.') 
     { 
      cin.clear(); 
      cin.ignore(100000, '\n'); 
     } 
     if(cin >> fighterType) 
     { 
      if(fighterType < 1 || fighterType > 5) 
      { 
       cout << "You did not enter a valid choice. Please try again." << endl; 
       cin.clear(); 
       cin.ignore(100000, '\n'); 
      } 
      else 
       break; 
     } 
     else 
     { 
      cout << "You did not enter a valid choice. Please try again." << endl; 
      cin.clear(); 
      cin.ignore(100000, '\n'); 
     } 
    } 
    //Now that we have the desired type of the fighter we must add a fighter of the correct type to the linked list 
    //representing team 2. We will do so by calling the add function of the linked list 
    cout << "Please enter the name of this fighter." << endl; 
    getline(cin, fighterName); 
    if(fighterType == 1) 
    { 
     team2.addBack("Barbarian", fighterName); 
    } 
    else if(fighterType == 2) 
    { 
     team2.addBack("BlueMen", fighterName); 
    } 
    else if(fighterType == 3) 
    { 
     team2.addBack("Vampire", fighterName); 
    } 
    else if(fighterType == 4) 
    { 
     team2.addBack("Medusa", fighterName); 
    } 
    else 
    { 
     team2.addBack("HarryPotter", fighterName); 
    } 
} 


cout << "Team 2 has been created!" << endl << endl; 

cout << "Let the fight begin!" << endl << endl; 

return 0; 

}

내 코드의 입력의 마지막 조각 간단히 Y가 입력되면 기능을 실행 한 후 또는 N Y 중 어느 하나의 문자를 입력하도록 요구하고, 다음과 같다.

cout << "Would you like to see the contents of the loserPile?" << endl; 
    cout << "Please enter y for yes or n for no" << endl; 
    for(;;) 
    { 
     if(cin >> displayLosers) 
     { 
      if(displayLosers != 'y' && displayLosers != 'n') 
      { 
       cout << "You did not enter either y or n. Please try again." << endl; 
       cin.clear(); 
       cin.ignore(100000, '\n'); 
      } 
      else 
       break; 
     } 
     else 
     { 
      cout << "You did not enter either y or n. Please try again." << endl; 
      cin.clear(); 
      cin.ignore(100000, '\n'); 
     } 
    } 
    if(displayLosers == 'y') 
    { 
     losers.displayPile(); 
    } 

내가 사용자 입력을 획득 오류를 만드는 중이라서 어디에서 누군가가 지적 할 수 있다면 나는 내가 알고있는 것을 시도하는 일에서 실행하고 나는 그것을 감사하겠습니다.

+0

코드가 너무 많습니다. [mcve]를 입력하십시오. 'getline'에 문제가 있음을 증명하는 데는 5 줄의 코드가 충분해야합니다. – user463035818

+0

[포맷 된 추출 후 std :: getline()이 입력을 건너는 이유는 무엇입니까?] (https://stackoverflow.com/questions/21567291)/왜 stdgetline-skip-after-a-formatted-extraction) – user0042

+0

문자열 변수의 유형은 무엇입니까? 문자 배열 또는'std :: string' 또는 다른 데이터 유형입니까? –

답변

0

당신은 "1."1에 입력 문자열을 변환합니다 if(cin.get() == '.')

>> 연산자를 추가하여 많은 문제를 만드는, 당신은 ignore(...,'\n').를 호출하는 경우 \n 전에 다른 문자는 무시됩니다. if(cin >> number){...}에 대한 테스트도 필요하지 않습니다. 당신은 오류를 나타 내기 위해 -1에 값을 초기화 할 수 있습니다 :

int menuChoice; 
for(;;) 
{ 
    menuChoice = -1; 
    cin >> menuChoice; 
    cout << menuChoice; 
    cin.clear(); 
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
    if(menuChoice == 1 || menuChoice == 2) 
    { 
     cout << menuChoice << "\n"; 
     break; 
    } 
    cout << "You did not enter a valid menu option. Please try again." << endl; 
} 

그냥 당신이 바로 입력을 사용하고 있는지 확인합니다. 예 또는 아니오 옵션의 경우 입력은 char이어야합니다.

cout << "enter y or n\n"; 
for(;;) 
{ 
    char val; 
    cin >> val; 
    if(val != 'y' && val != 'n') 
    { 
     cout << "You did not enter either y or n. Please try again." << endl; 
     cin.clear(); 
     cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
    } 
    else 
     break; 
} 
+0

대다수의 문제는 cin()과 같았습니다. 특히 인증 코드에 어디에 위치 시켰는지 말 했으니까요. –